Requirement: There is an add button on the page. Each time you click this button, a webpartzone is dynamically created on the page!
Reminder: webpartzone can only be created before oninit or. Otherwise, an exception is reported!
As we all know, the button clicking event is triggered at raisepostbackevent, which means that the clicking event is executed after the onload stage, far behind the oninit stage, and the viewstate is ready at the onload stage, viewstate cannot be used for oninit and earlier stages! If you try to create controls such as webpartzone in the button click event, the only consequence is that a page error occurs. If you create a control in oninit, because viewstate is not ready, some data, such as the number of files to be created (in viewstate), cannot be obtained!
At present, I have not found any good solutions for this problem. After experiment, I barely come up with a very elegant solution, that is, using hiddenfield to store data and then directly use request. form ["XXX"] obtains data in the oninit phase, and determines whether to click the button through the request. whether the corresponding data exists in the form! Let's take a look. Code Right! Copy code The Code is as follows: Private void page_load (Object sender, system. eventargs E)
{
Button button1 = new button ();
Button1.commandargument = "B1 ";
Button1.text = "btn1 ";
Button1.command + = new commandeventhandler (this. onbutton );
Placeholder1.controls. Add (button1 );
Button button2 = new button ();
Button2.commandargument = "B2 ";
Button2.text = "btn2 ";
Button2.command + = new commandeventhandler (this. onbutton );
Placeholder1.controls. Add (button2 );
Control C3 = parsecontrol ("<asp: button id = 'button3' text = 'btn3' commandname = 'btn 'commandargument = 'b3 'runat = 'server'/> "); // convert a string to a Web Control
Control C4 = parsecontrol ("<asp: button id = 'button4' text = 'btn4' commandname = 'btn 'commandargument = 'b4 'runat = 'server'/> ");
Placeholder1.controls. Add (C3 );
Placeholder1.controls. Add (C4 );
Button mybut = (button) page. findcontrol ("button3 ");
Mybut. Command + = new commandeventhandler (this. onbutton );
Button mybut2 = (button) page. findcontrol ("button4 ");
Mybut2.command + = new commandeventhandler (this. onbutton );
}
Public void onbutton (Object sender, commandeventargs E)
{
Switch (E. commandargument. tostring (). tolower ())
{
Case "B1 ":
Label1.text = "button 1 ";
Break;
Case "B2 ":
Label1.text = "button 2 ";
Break;
Case "B3 ":
Label1.text = "button 3 ";
Break;
Case "B4 ":
Label1.text = "button 4 ";
Break;
};
}
Asp.net dynamically adds events to controls
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:Copy codeThe Code is as follows: Control. Command + = new commandeventhandler (this. eventfun );
For specific code, see 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.Copy codeThe Code is as follows: 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 = 'mycall' commandargument =' B 'runat = 'server'/> ");
// Add the widget to the 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 + = new commandeventhandler (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> ");
}