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> " );
}