The general method is to use the itemcommand event of repeat (DataGrid, datalist, and gridview are the same), as shown in
// . Aspx
< ASP: repeater ID = " Repeater1 " Runat = " Server " Onitemcommand = " Repeaterpolicitemcommand " >
< Itemtemplate >
< ASP: button ID = " BTN " Commandname = " Sayhello " Runat = " Server " Text = " Sayhello " />
</ Itemtemplate >
</ ASP: Repeater >
// . Aspx. CS
Protected Void Repeaterincluitemcommand ( Object Source, repeatercommandeventargs E)
{
Switch (E. commandname ){
Case " Sayhello " :
Response. Write (E. Item. itemindex );
Response. Write (E. commandargument ); // Custom parameter information
Break ;
}
}
3.
Directly set the button click event, of course, to processProgramYou need some skills to obtain other information, such:
// . Aspx
< ASP: repeater ID = " Repeater2 " Runat = " Server " Onitemcommand = " Repeaterpolicitemcommand " >
< 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 that triggers the event
Button BTN = Sender As Button;
// Row of Button
Repeateritem = BTN. namingcontainer As Repeateritem;
}
Protected Void Btn2inrepeat2_click ( Object Sender, eventargs E)
{
// Button that triggers the event
Htmlbutton BTN = Sender As Htmlbutton;
// Row of Button
Repeateritem = BTN. namingcontainer As Repeateritem;
}
4.
For buttons, we recommend that you use the webcontrol button instead of runat.=Serer input,
You can see that the button has commandname and commandarguments.
Hope helpful!
1. HTML Web controls is not recommended
2. If you place a button in itemtemplate in repeater, you can use the itemcommand event to process the event. You do not need to register the event yourself.