关于repeater内控件的事件绑定,内部模板列
比较常规的方法是 使用Repeat(DataGrid,DataList,GridView均一样) 的 ItemCommand 事件,如
// .aspx
<asp:Repeater ID= "Repeater1 " runat= "server " OnItemCommand= "Repeater1_ItemCommand ">
<ItemTemplate>
<asp:Button ID= "btn " CommandName= "SayHello " runat= "server " Text= "SayHello " />
</ItemTemplate>
</asp:Repeater>
// .aspx.cs
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
switch (e.CommandName) {
case "SayHello ":
Response.Write(e.Item.ItemIndex);
Response.Write(e.CommandArgument); // 自定义的参数信息
break;
}
}
3.
直接设置 button 的 click 事件, 当然处理程序需要一点技巧才能获取其他信息, 如:
// .aspx
<asp:Repeater ID= "Repeater2 " runat= "server " OnItemCommand= "Repeater1_ItemCommand ">
<ItemTemplate>
<asp:Button ID= "btn " runat= "server " CommandArgument= ' <%# Eval( "SomeFieldName ") %> ' Text= "SayHello " OnClick= "btnInRepeat2_Click " />
<input id= "btn2 " runat= "server " runat= "server " value= "SayHello " onclick= "btn2InRepeat2_Click " />
</ItemTemplate>
</asp:Repeater>
// .aspx.cs
protected void btnInRepeat2_Click(object sender, EventArgs e)
{
// 触发事件的 Button
Button btn = sender as Button;
// Button 所在行
RepeaterItem item = btn.NamingContainer as RepeaterItem;
}
protected void btn2InRepeat2_Click(object sender, EventArgs e)
{
// 触发事件的 Button
HtmlButton btn = sender as HtmlButton;
// Button 所在行
RepeaterItem item = btn.NamingContainer as RepeaterItem;
}
4.
对于 button ,建议使用 WebControl 的 Button 而不是使用 runat=serer 的 input,
你看到了 button 具有 CommandName 和 CommandArguments 可以使用
Hope helpful!
// .aspx
<asp:Repeater ID= "Repeater1 " runat= "server " OnItemCommand= "Repeater1_ItemCommand ">
<ItemTemplate>
<asp:Button ID= "btn " CommandName= "SayHello " runat= "server " Text= "SayHello " />
</ItemTemplate>
</asp:Repeater>
// .aspx.cs
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
switch (e.CommandName) {
case "SayHello ":
Response.Write(e.Item.ItemIndex);
Response.Write(e.CommandArgument); // 自定义的参数信息
break;
}
}
3.
直接设置 button 的 click 事件, 当然处理程序需要一点技巧才能获取其他信息, 如:
// .aspx
<asp:Repeater ID= "Repeater2 " runat= "server " OnItemCommand= "Repeater1_ItemCommand ">
<ItemTemplate>
<asp:Button ID= "btn " runat= "server " CommandArgument= ' <%# Eval( "SomeFieldName ") %> ' Text= "SayHello " OnClick= "btnInRepeat2_Click " />
<input id= "btn2 " runat= "server " runat= "server " value= "SayHello " onclick= "btn2InRepeat2_Click " />
</ItemTemplate>
</asp:Repeater>
// .aspx.cs
protected void btnInRepeat2_Click(object sender, EventArgs e)
{
// 触发事件的 Button
Button btn = sender as Button;
// Button 所在行
RepeaterItem item = btn.NamingContainer as RepeaterItem;
}
protected void btn2InRepeat2_Click(object sender, EventArgs e)
{
// 触发事件的 Button
HtmlButton btn = sender as HtmlButton;
// Button 所在行
RepeaterItem item = btn.NamingContainer as RepeaterItem;
}
4.
对于 button ,建议使用 WebControl 的 Button 而不是使用 runat=serer 的 input,
你看到了 button 具有 CommandName 和 CommandArguments 可以使用
Hope helpful!
1. 不推荐使用Html Web Controls
2. 如果在Repeater中ItemTemplate中放的按钮,一般使用ItemCommand事件处理即可,无需自己再注册事件