Obtain values and controls in the Repeater control

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.