Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Find Controls In <ItemTemplate> Repeater

I have this source code:

<div id = "AddComment">
    <asp:TextBox ID="txtComment" runat="server" TextMode="MultiLine" Height="20"></asp:TextBox>
    <asp:Button ID="btnComment" CommandName="btnComment_click"  runat="server" Text="Comment" />                           
</div>

and it's inside an item template tag for an ASP Repeater ... what i want to do is making c# code for some events for these two controls .. the text box and the button ... how can i reach these controls from the c # code ?

like image 323
Bavaria Avatar asked Jan 17 '23 17:01

Bavaria


1 Answers

You need to hook to the OnItemDataBound

<asp:Repeater OnItemDataBound="RepeaterItemEventHandler" ... />

Now, on code behind....

  void RepeaterItemEventHandler(Object Sender, RepeaterItemEventArgs e) {
     if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
        TextBox currentTextBox = (TextBox)e.Item.FindControl("txtComment");
        //do something cool 
     }
  }
like image 154
Icarus Avatar answered Jan 28 '23 00:01

Icarus