-
Because I switched from ASP to learning. net, a lot of ideas have to follow ASP, but in my just getting started, I found that the display data is generally bound to a dataset control (such as, repeater ), displaying Data in repeater is also easy. You only need to write <% # eval ("field name") %> In itemtemplate.
But sometimes we need to dynamically attach the field content to a container (such as label) in the CS file, but in the background, enter label. TEXT = eval ("field name") is not acceptable
There are two problems:
1. The Labe in repeater is in the CS file, so it cannot be directly attached with a value (the value retrieved from the database)
2. eval ("field name") cannot be written.
What should we do?
In fact, you can use the following method to attach a value to label1 (belowCodeE is the transfer parameter in the event, which is generated by the system)
If (E. Item. itemtype = Listitemtype. Item | E. Item. itemtype = Listitemtype. alternatingitem)
{
(Label) E. Item. findcontrol ( " Label1 " ). Text = (Datarowview) E. Item. dataitem). Row [ " Adate " ]. Tostring ();
}
---------------------------------------------
Another problem is that in ASP, all records are displayed in the HTML loop by ourselves. We can add a lot of code to control the loop.
In. net, the database content is basically displayed by the control itself. We do not need to write loop Code such as ASP in. Net:
< %
Do While Not Rs. EOF
Rs. movenext
loop
% >
what if we need to add the control code in the loop like asp?
we can use the itemcreated event. In repeater, this event is automatically executed every time a record is displayed.