SoFunction
Updated on 2025-04-08

Dynamically added controls and add events in the page, page 1/2

Requirements: There is an Add button on the page. Every time the button is clicked, a WebPartZone is dynamically created on the page!

Reminder: WebPartZone can only be created before OnInit or before, otherwise an exception will be reported!

As we all know, the button click event is fired when the RaisePostbackEvent, which means that the click event is executed after the OnLoad stage, far behind the OnInit stage, and ViewState is only ready when OnLoad, OnInit and previous stages cannot use ViewState at all! If you try to create controls such as WebPartZone in the button click event, the only consequence is that a page error occurs; and if you create a control in OnInit, since ViewState is not ready, some data, such as the number of currently needed to be created (inherited in ViewState) cannot be obtained!

At present, I have not found any good solution to this problem. After experimenting, I barely came up with a not-so-elegant solution, which is to use HiddenField to save the data, and then use ["XXX"] to obtain the data in the OnInit stage; and determining whether to click the button is also determined by whether the corresponding data exists! I won’t say much nonsense, let’s take a look at the code!
Copy the codeThe code is as follows:

private void Page_Load(object sender, e)
{
Button Button1 = new Button();
= "b1";
= "Btn1";
+= new CommandEventHandler();
(Button1);
Button Button2 = new Button();
= "b2";
= "Btn2";
+= new CommandEventHandler();
(Button2);
Control c3 = ParseControl("<asp:Button id='Button3' text='Btn3' commandname='Btn' commandargument='b3' runat='server' />"); //Convert string to web control
Control c4 = ParseControl("<asp:Button id='Button4' text='Btn4' commandname='Btn' commandargument='b4' runat='server' />");
(c3);
(c4);
Button myBut = (Button)("Button3");
+= new CommandEventHandler();
Button myBut2 = (Button)("Button4");
+= new CommandEventHandler();

}
public void OnButton(Object Sender, CommandEventArgs e)
{
switch (().ToLower())
{
case "b1":
= "Button 1";
break;
case "b2":
= "Button 2";
break;
case "b3":
= "Button 3";
break;
case "b4":
= "Button 4";
break;
};
}

Dynamically add events to the control
The implementation function is to dynamically add a Button to the Panel on the web page and write a click event for this Button.
Dynamically add control events and statements:
Copy the codeThe code is as follows:

+= new CommandEventHandler();

For specific codes, please see below:
Special attention should be paid to:
When adding controls and adding events to controls, you must not put them in if (!IsPostback){}. In that case, the control will disappear after clicking once, and the event will not be
Will execute.
Copy the codeThe code is as follows:

protected void Page_Load(object sender, EventArgs e)
{
//Analyze the input string into an object, b is the value passed in
Control c = ParseControl("<asp:Button Text = 'press me' ID = 'myButton' commandargument = 'b' runat = 'server' />");

//Add control into a large panel
this.(c);

// Find the control with the page named myButton
Button Button = (Button)("myButton");

//Add event On_Button
+= new CommandEventHandler(this.On_Button);

}

//CommandEventArgs provides data for command events
protected void On_Button(Object sender,CommandEventArgs e)
{
("<script language = 'javascript' type = 'text/javascript'><!--
alert('" + () + "');
// --></script>");
}

12Next pageRead the full text