Asp.net dynamically adds events to controls

Source: Internet
Author: User
The function is to dynamically Add a button in the panel on the webpage and write a click event for the button.

Dynamically add control events. Statement:

Code
Control. Command+ = NewCommandeventhandler (This. Eventfun );

SpecificCodeSee the following:

Note the following:

When adding controls and adding events to controls, you cannot place them in if (! Ispostback) {}. In that case, the control disappears after one click, and the event does not exist.

Yes.

Code
Protected   Void Page_load ( Object Sender, eventargs E)
{
// Analyze the input string as a system. Web. UI. control object, and B as the passed value.
Control C = Parsecontrol ( " <Asp: button text = ''Id = 'myclick' commandargument =' B 'runat = 'server'/> " );

//Adding a widget to a large panel
This. Panel1.controls. Add (C );

//Find the control named mybutton on the page
Button button=(Button) page. findcontrol ("Mybutton");

//Add event on_button
Button. Command+ = NewCommandeventhandler (This. On_button );

}

// Commandeventargs provides data for command events
Protected   Void On_button (Object sender, commandeventargs E)
{
Response. Write ( " <Script language = 'javascript 'Type = 'text/JavaScript '> alert (' "   + E. commandargument. tostring () +   " '); </SCRIPT> " );
}

Related Article

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.