How To Find Controls In <ItemTemplate> Repeater

时光总嘲笑我的痴心妄想 提交于 2019-12-10 09:31:53

问题


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 ?


回答1:


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 
     }
  }


来源:https://stackoverflow.com/questions/10544123/how-to-find-controls-in-itemtemplate-repeater

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!