Home » General

Finding Controls in Repeater/DataList From The Event Handler

20 June 2009 971 views No Comment

You may have Button, DropDownList etc controls in a Repeater/DataList that generates an Event. From those event handlers how do you find other controls inside the Repeater/DataList, in case you need the values from other controls.

Here in this example below we have a button and a textbox inside a Repeater. The Button have an OnClick event. Inside that event handler we want to get the value from the TextBox.

<asp:repeater id=”rpt”……>
<ItemTemplate>
<asp:TextBox id=”txt” runat=”server” />
<asp:Button id=”btn” Text=”TEST” onclick=”btn_Click” runat=”server”/>
</ItemTemplate>
</asp:Repeater>

protected void btn_Click(object sender, System.EventArgs e)
{
Button btn=(Button)sender;
Repeater rpt=(Repeater)btn.Parent;
//now you can use this rpt to find any control inside it
//For example
TextBox txt=(TextBox)rpt.FindControl(“txt”);
string val=txt.Text;
}

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.