Programmatically accessing Data in an ASP.NET 2.0 Repeater

Question :

Programmatically accessing Data in an ASP.NET 2.0 Repeater,

Answer :

This is an ASP.Net 2.0 web app. The Item template looks like this, for reference:

                <%# DataBinder.Eval(Container.DataItem,"field1") %>      <%# DataBinder.Eval(Container.DataItem,"field2") %>      <%# DataBinder.Eval(Container.DataItem,"field3") %>      <%# DataBinder.Eval(Container.DataItem,"field4") %>        

Using this in codebehind:

foreach (RepeaterItem item in rptrFollowupSummary.Items)  {      string val = ((DataBoundLiteralControl)item.Controls[0]).Text;      Trace.Write(val);  }  

I produce this:

        23    1/1/2000    -2    11    

What I need is the data from Field1 and Field4

I can’t seem to get at the data the way I would in say a DataList or a GridView, and I can’t seem to come up with anything else on Google or quickly leverage this one to do what I want. The only way I can see to get at the data is going to be using a regex to go and get it (Because a man takes what he wants. He takes it all. And I’m a man, aren’t I? Aren’t I?).

Am I on the right track (not looking for the specific regex to do this; forging that might be a followup question 😉 ), or am I missing something?

The Repeater in this case is set in stone so I can’t switch to something more elegant. Once upon a time I did something similar to what Alison Zhou suggested using DataLists, but it’s been some time (2+ years) and I just completely forgot about doing it this way. Yeesh, talk about overlooking something obvious. . .

So I did as Alison suggested and it works fine. I don’t think the viewstate is an issue here, even though this repeater can get dozens of rows. I can’t really speak to the question if doing it that way versus using the instead (but that seems like a fine solution to me otherwise). Obviously the latter is less of a viewstate footprint, but I’m not experienced enough to say when one approach might be preferrable to another without an extreme example in front of me. Alison, one question: why literals and not labels?

Euro Micelli, I was trying to avoid a return trip to the database. Since I’m still a little green relative to the rest of the development world, I admit I don’t necessarily have a good grasp of how many database trips is “just right”. There wouldn’t be a performance issue here (I know the app’s load enough to know this), but I suppose I was trying to avoid it out of habit, since my boss tends to emphasize fewer trips where possible.


Off the top of my head, you can try something like this:


Then, in your code behind, you can access each Literal control as follows:

foreach (RepeaterItem item in rptrFollowupSummary.Items)  {         Literal lit1 = (Literal)item.FindControl("litField1");      string value1 = lit1.Text;      Literal lit4 = (Literal)item.FindControl("litField4");      string value4 = lit4.Text;  }  

This will add to your ViewState but it makes it easy to find your controls.

That’s the answer Programmatically accessing Data in an ASP.NET 2.0 Repeater, Hope this helps those looking for an answer. Then we suggest to do a search for the next question and find the answer only on our site.

Disclaimer :

The answers provided above are only to be used to guide the learning process. The questions above are open-ended questions, meaning that many answers are not fixed as above. I hope this article can be useful, Thank you

Read More  Difference between NULL and null in PHP