Requirements: There is an Add button on the page, each click on the button, dynamically create a webpartzone! on the page
Reminder: WebPartZone can only be created before oninit or the report is abnormal!
As we all know, button Click events are triggered when raisepostbackevent, which means that the click event is executed after the onload phase, far behind the OnInit stage, and ViewState is ready when the onload OnInit and the previous stage simply cannot use viewstate! If you try to create a WebPartZone control in the button click event, the only consequence is a page error, and if you create the control inside the OnInit, because ViewState is not ready, Then some data, such as the number of current needs to be created (inside viewstate) can not be obtained!
At present, I have not found any good solution to this problem, after the experiment, reluctantly came up with a less elegant solution is to use HiddenField to save data, and then directly use request.form["XXX"] in the OnInit phase to obtain data and determine whether the Click button is also through the Request.Form whether there is a corresponding data to judge! Nonsense don't say much, everybody look code!
Copy Code code 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 add events to controls dynamically
The function is to dynamically add a button to the Panel on the Web page and write a click event for the button.
To dynamically add a control's event, statement:
Copy Code code as follows:
Control.command + = new Commandeventhandler (this. Eventfun);
For specific code, see below:
Special attention needs to be paid to:
You must never place an if (!) when adding controls and adding events to controls. IsPostback) {} inside, in that case, once clicked, the control disappears and the event does not
Will execute.
Copy Code code as follows:
protected void Page_Load (object sender, EventArgs e)
{
Parse the input string into the System.Web.UI.Control object and B to the value passed in
Control C = Parsecontrol ("<asp:button Text = ' press my ' ID = ' myButton ' commandargument = ' b ' runat = ' server '/>");
Add a control to a large panel
This. PANEL1.CONTROLS.ADD (c);
Find a control with a page name of MyButton
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> ");
}
current 1/2 page
1 2 Next read the full text