Programmatically Access Data in ASP.NET 2.0 Repeater

This is an ASP.Net 2.0 web application. The element template is as follows:

<ItemTemplate> <tr> <td class="class1" align=center><a href='url'><img src="img.gif"></a></td> <td class="class1"><%# DataBinder.Eval(Container.DataItem,"field1") %></td> <td class="class1"><%# DataBinder.Eval(Container.DataItem,"field2") %></td> <td class="class1"><%# DataBinder.Eval(Container.DataItem,"field3") %></td> <td class="class1"><%# DataBinder.Eval(Container.DataItem,"field4") %></td> </tr> </ItemTemplate> 

Using this in codebehind:

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

I create this:

 <tr> <td class="class1" align=center><a href='url'><img src="img.gif"></a></td> <td class="class1">23</td> <td class="class1">1/1/2000</td> <td class="class1">-2</td> <td class="class1">11</td> </tr> 

I need data from Field1 and Field4

It seems I can’t get the data as I would say, DataList or GridView, and I can’t come up with anything else on Google or use it quickly to do what I want. The only way I can get the data is to use a regular expression to go and get it (because the person takes what he wants. He takes it all. And I'm human, right? Me?).

Am I on the right track (not looking for a specific regular expression to do this, fake, which may be the next question;)), or am I missing something?


Repeater in this case is set in stone, so I can not switch to something more elegant. Once I did something similar to what Alison Zhou suggested using DataLists, but it was some time (2+ years), and I just completely forgot about it. Yish, talk about skipping something obvious.,.

So, I did what Alison suggested, and everything works fine. I do not think that there is a problem in this view, although this relay can receive dozens of lines. I can't answer the question if you do it this way against using it instead (but this seems like a great solution for me otherwise). Obviously, the latter has less visibility, but I'm not experienced enough to say when one approach may be preferable to another without an extreme example in front of me. Alison, one question: why are literals and not shortcuts?

Euro Micelli, I tried to avoid a trip back to the database. Since I'm still a little green relative to the rest of the development world, I admit that I don’t necessarily know how many flights to the database are “correct”. There would be no performance issue (I know that the application is enough to know this), but I believe that I tried to avoid this out of habit, as my boss tends to emphasize fewer trips where possible.

+4
source share
5 answers

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

 <ItemTemplate> <tr> <td "class1"><asp:Literal ID="litField1" runat="server" Text='<%# Bind("Field1") %>'/></td> <td "class1"><asp:Literal ID="litField2" runat="server" Text='<%# Bind("Field2") %>'/></td> <td "class1"><asp:Literal ID="litField3" runat="server" Text='<%# Bind("Field3") %>'/></td> <td "class1"><asp:Literal ID="litField4" runat="server" Text='<%# Bind("Field4") %>'/></td> </tr> </ItemTemplate> 

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 your ViewState, but it will make it easier to find your controls.

+6
source

Since you are working with tabular data, I would recommend using the GridView control. Then you can access individual cells.

Otherwise, you can set td for Field1 and Field4 to runat="server" and give them identifiers. Then, in code, enter access to the InnerText property for each td.

+2
source

If you can afford to get more overhead in the generation, go to the DataList and use the DataKeys property, which will save the required data fields.

You can also use labels in each cell of the table and be able to reference elements using e.Item.FindControl ("LabelID").

0
source

<% # DataBinder.Eval (...)%> The mechanism is not data binding in the "strict" sense. This is a one-way technique for placing text in specific places in a template.

If you need to return data, you must either:

  • Get it from the source data
  • Fill the repeater using another mechanism.

Note that Repeater does not store a DataSource between postbacks, you cannot just ask it to provide you data later.

The first method is usually easier to work with. Do not assume that it is too expensive to recover your data from the source unless you prove it yourself by measuring; it's usually pretty fast. The biggest problem with this method is that the source data can change between calls.

For the second method, the use of Literal control is generally accepted. See Alison Zhou for an example of how to do this. I usually prefer to populate literal controls inside OnItemDataBound instead

0
source

@peacedog :

Right; Alison's method is quite acceptable.

Database callbacks trick: they are not free, obviously, but web servers tend to be very “close” (fast, low latency connections) to the database, while your users are probably “far away” ( slow, high latency).

Because of this, sending data to / from the browser via cookies, ViewState, hidden fields or any other method can really be “worse” than reading it from your database. There are also security implications to keep in mind (can an “evil” user fake data returned from the browser? Would it be important if they did?).

But often this does not affect performance. This is why you should do what works more naturally for your particular problem and worry about it, but only if performance becomes a real problem.

Good luck

0
source

Source: https://habr.com/ru/post/1276629/


All Articles