Click to See Complete Forum and Search --> : C# .NET repeater question


mjacobson
May 20th, 2005, 02:59 PM
I am working on a web application in C# and have set up a repeater to display a list of items. One of the fields being returned from the SQL database is a list of terms seperated by a space. I need to be able to split each of these terms so I can make a link to another pages on the site.

I have been trying to do this in the "ItemTemplate" declaration with the following code segment

//string taglist = "test test2 test3 test4";
string taglist = DataBinder.GetPropertyValue(Container.DataItem, "tag_list");
foreach(string tag in taglist.Split(' '))
{
Response.Write(tag + "<BR>");
}

This produces the following error,

The type or namespace name 'Container' could not be found (are you missing a using directive or an assembly reference?)

When I use <%#DataBinder.Eval(Container.DataItem, "tag_list")%> I get the string printed out to the web page with no errors. Is there a way to set this equal to a temp string variable so I can split it and produce the output that I need?

Thanks for your help. I am looking forward to the answer.

mmetzger
May 20th, 2005, 04:44 PM
If I understand you correctly, the easiest way is to create a method that parses the data as you see fit and call that from the repeater. For example:


string createHyperlink(string rawlink)
{

return "<a href=\"" + rawlink + "/html/\">Link!</a>";
}


Then in the repeater's Item Template, add:


<%# createHyperlink(DataBinder.Eval(Container.DataItem, "rawlink").ToString()) %>


You can of course modify the parameters and return values as you need to.

subdigital
May 24th, 2005, 08:50 PM
For any values that are not just plain text, I generally like to specify their values during databinding in the code. This way I have intellisense and compile-time errors rather than run-time errors.

To do this, in your item template you specify a label as the control that you will populate with your data. Then, simply capture the ItemDataBound event of the repeater and handle it in one of your methods.




private void datgrid_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if( e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
//do this so that you do not capture the header and footer as part of the binding
Label myLabel = e.Item.FindControl("myLabel") as Label;
foreach(string s in Arr.Split(' '))
myLabel.Text += s + "<br>";
}
}



This is a little more code, but we are moving all of our logic outside of the presentation, and that is always a good thing. Generally people (and most beginner books for that matter) put code directly in the html because it is easy to read and people are used to it (from class ASP, PHP, whatever). I don't like this practice at all because if you go to debug your code, you have to look in 2 places to find the culprit, whereas with the above method, you only have to look in 1, (and you get to step through the code! can't step through html... :lol: )

Hope this helps. :D