Showing posts with label datalist. Show all posts
Showing posts with label datalist. Show all posts

Thursday, March 29, 2012

newbie: ItemDataBound and XPath

hey

asp.net 2.0

I have a DataList which has a XmlDataSource as its data source.

This code crash because I use XPath in the ItemDataBound event:
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs
e)
{
Label label = (Label)e.Item.FindControl("lblTitle");
label.Text = XPath("title").ToString();
label = (Label)e.Item.FindControl("lblStory");
label.Text = XPath("description").ToString();
}

Any suggestion how this should be done in a ItemDataBound event?

Jeff"Jeff" <it_consultant1@.hotmail.com.NOSPAMwrote in message
news:eCAL$KttGHA.2160@.TK2MSFTNGP04.phx.gbl...

Quote:

Originally Posted by

hey
>
asp.net 2.0
>
I have a DataList which has a XmlDataSource as its data source.
>
This code crash because I use XPath in the ItemDataBound event:
protected void DataList1_ItemDataBound(object sender,
DataListItemEventArgs e)
{
Label label = (Label)e.Item.FindControl("lblTitle");
label.Text = XPath("title").ToString();
label = (Label)e.Item.FindControl("lblStory");
label.Text = XPath("description").ToString();
}
>
Any suggestion how this should be done in a ItemDataBound event?
>
Jeff
>


Jeff look at this sample here at:-
http://www.msdner.com/forum/thread467894.html
Patrick

"Jeff" <it_consultant1@.hotmail.com.NOSPAMwrote in message
news:eCAL$KttGHA.2160@.TK2MSFTNGP04.phx.gbl...

Quote:

Originally Posted by

hey
>
asp.net 2.0
>
I have a DataList which has a XmlDataSource as its data source.
>
This code crash because I use XPath in the ItemDataBound event:
protected void DataList1_ItemDataBound(object sender,
DataListItemEventArgs e)
{
Label label = (Label)e.Item.FindControl("lblTitle");
label.Text = XPath("title").ToString();
label = (Label)e.Item.FindControl("lblStory");
label.Text = XPath("description").ToString();
}
>
Any suggestion how this should be done in a ItemDataBound event?
>
Jeff
>

Friday, March 16, 2012

Newline in a multiline-textbox filled by a DataList?

I'm binding my Data to the Datalist like that:

// Opening standard XML text reader
System.Xml.XmlTextReader myXmlReader = new System.Xml.XmlTextReader(Server.MapPath("~/xml/guestbook.xml"));
DataSet myDataSet = new DataSet();
// Reading XML into a dataset
myDataSet.ReadXml(myXmlReader);
myXmlReader.Close();

// Bind data to the data list!
GuestbookData.DataSource = myDataSet.Tables[0];
GuestbookData.DataBind();

then I read it out like that:

<div class="author"><a href="http://links.10026.com/?link=http://<%# DataBinder.Eval(Container.DataItem, "url") %>" target="_blank"><%# DataBinder.Eval(Container.DataItem, "name") %></a> schrieb am <%# DataBinder.Eval(Container.DataItem, "date") %> </div>
<div class="text"><%# DataBinder.Eval(Container.DataItem, "entry_Text") %></div>

Now I'd like to translate NewLines a User made when he was writing a comment into <br>s because right now, such a text:

writing something

and something else

is shown as writing something and something else. (In just one line without the linebreak).

How to translate those breaks? Thanks!

Change:
<div class="text"><%# DataBinder.Eval(Container.DataItem, "entry_Text") %></div>
to:
<div class="text"><%# ReplaceCRLF(DataBinder.Eval(Container.DataItem, "entry_Text")) %></div>
and then have a function in your page called ReplaceCRLF:
string ReplaceCRLF(string val)
{
return val.Replace(System.Environment.NewLine, "<br/>");
}
-Brock

Thanks, that's working!