Event binding of controls in repeater, internal template Column

Source: Internet
Author: User

 

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.