我有这个源代码:
<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>
它位于ASP Repeater的项目模板标签内... 我想要做的是为这两个控件的某些事件制作c#代码..文本框和按钮......如何从c#代码中获取这些控件?
答案 0 :(得分:9)
您需要挂钩OnItemDataBound
<asp:Repeater OnItemDataBound="RepeaterItemEventHandler" ... />
现在,代码背后......
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
}
}