I posted a blog post about how to capture child controls in grieview. However, the gridview is too large, and it seems too cumbersome to pursue efficiency today. The requirement for capturing sub-controls in repeater exists. Therefore, after reading the relevant documents and trying several failed attempts, I learned some of the following experiences and gains to share with you.
First, let's take a look at the knowledge involved in implementing the above functions. Refer to msnd.
- RepeateritemObject RepresentationRepeaterItems in the control, such as the title part, footer part, or data item.
RepeaterThe data items of the control are stored in the repeateritemcollection object.RepeaterThe items attribute of the control can access this object.
AvailableRepeateritemObjects are accessed programmatically.RepeaterProperties of an item in the control.
Msdn sample analysis
<% @ Page Language = "C #" autoeventwireup = "true" %>
<% @ Import namespace = "system. Data" %>
<HTML>
<Head>
<Script language = "C #" runat = "server">
Void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
Arraylist values = new arraylist ();
Values. Add (newpositiondata ("item 1", "$6.00 "));
Values. Add (newpositiondata ("item 2", "$7.48 "));
Values. Add (newpositiondata ("item 3", "$9.96 "));
// Prepare data for repeater. The core step is that only data has been bound to repeater.
// The required items and controls may be obtained.
Repeater1.datasource = values;
Repeater1.databind ();
}
}
Void button_click (Object sender, eventargs E)
{
Label1.text = "the items collection contains: <br> ";
Foreach (repeateritem item in repeater1.items)
{// Read the value of the corresponding item
Label1.text + = (databoundliteralcontrol) item. controls [0]). Text +
"<Br> ";
// Obtain the control
Label2.text + = (Label) item. controls [0]. findcontrol ("lable2"). Text +
"<Br> ";
}
}
Public class positiondata
{
Private string item;
Private string price;
Public positiondata (string item, string price)
{
This. Item = item;
This. Price = price;
}
Public String item
{
Get {return item ;}
}
Public String price
{
Get {return price ;}
}
}
</SCRIPT>
</Head>
<Body>
<Form runat = Server>
<H3> repeater example <P>
<Asp: repeater id = "repeater1" runat = "server">
<Headertemplate>
<Table border = 1>
<Tr>
<TD> <B> item </B> </TD>
<TD> <B> price </B> </TD>
<TD> <B> label </B> </TD>
</Tr>
</Headertemplate>
<Itemtemplate>
<Tr>
<TD> <% # databinder. eval (container. dataitem, "item") %> </TD>
<TD> <% # databinder. eval (container. dataitem, "price") %> </TD>
// Here we add a server-side Test Control
<TD> <asp: Label id = "lable2" runat = "server" text = '<% # databinder. eval (container. dataitem, "price") %> '> </ASP: Label> </TD>
</Tr>
</Itemtemplate>
<Footertemplate>
</Table>
</Footertemplate>
</ASP: repeater>
<P>
<Asp: button id = "button1" text = "display items in repeater" onclick = "button_click" runat = "server"/>
<Br>
<Asp: Label id = "label1" runat = "server"/>
</Form>
</Body>
</Html>
Conclusion: only when data is bound can the items and child controls of repeater be obtained.