Dynamic addition and deletion of events in server---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
PlaceHolder: The control container is not displayed by itself. It is mainly used to place server controls, and only displays its child elements (the controls inside it)
Dynamic addition and removal of server controls
<1> Dynamically add server-side controls in PlaceHolder
General ways to add controls:
Declare a new control such as label lb =new label()
Set control properties such as:="text"
Add controls to PlaceHolder, such as: (lb)
You can use ViewState[AddedControl]=null to determine whether it was the first execution
Dynamically add controls. If you need data binding, you need to add controls first and then data binding. After submitting (postback), you do not need to rebind.
Just go for a while, such as:
Copy the codeThe code is as follows:
if ((ViewState[AddedControl] != null) & ((bool)ViewState[AddedControl]))//Don't bind data the second time
{
();//Clear the control in ph1
DropDownList dpl = new DropDownList();
= "controlid";
= true;
(dpl);
}
else
{
();
DropDownList dpl = new DropDownList();
= "controlid";
= true;
(dpl);
//Link database, set data source, etc.
();
ViewState[AddedControl] = true;
}
<2> Dynamically add server-side controls to the panel
Add datagrid in a panel and bind data
Copy the codeThe code is as follows:
Panel panel1 = new Panel();
["top"] = "200px";//Set properties
(panel1);
DataGrid dg1 = new DataGrid();//Define datagrid
BoundColumn s1 = new BoundColumn();//Define a column
= "first";//Binding column data source
= "mzi";//This column title and other attribute settings
(s1);//Add to this column
= "#00000";
=3;
//Set dg1 properties, etc.
(dg1);
<3> Add events to dynamically added controls:
For example: Add an event to the button
Copy the codeThe code is as follows:
//Add Button 1
Button bt1 = new Button();
= "bt1";//Set command parameters
= "daji";
+= new CommandEventHandler();//Scheduled event
(bt1);
//Add Button 2, another method
Control cs = ParseControl("<asp:Button ID='Button2' runat='server' Text=Button' commandname='btn' CommandArgument = 'bt2'/>");//Convert strings into controls
(cs);
Button bt2 = (Button)("button2");
+= new CommandEventHandler();//Add event to bt2
}
public void onbutton(object sender, CommandEventArgs e)
{
= "label1";
}
In this way, both buttons correspond to a function and execute the same event
If you want them to perform different events,
You can write this way:
Copy the codeThe code is as follows:
public void onbutton(object sender, CommandEventArgs e)
{
switch (().ToLower()) gets command parameters and executes different commands according to the parameters
{
case "bt1": = "label1"; break;
case "bt2": = "label2"; break;
}
}
<IV>: Add rows and server-side controls in table
Add one row and two columns to the table to convert the server-side control
The code is as follows:
Copy the codeThe code is as follows:
HtmlTableRow tr1 = new HtmlTableRow();//Define the row
HtmlTableCell td1 = new HtmlTableCell();//Define the column
Label lb1 = new Label();// Define lb1 as a Label control
= txt[m];//lb1
(lb1);//Add lbl to the column
HtmlTableCell td2 = new HtmlTableCell();//Define the column
TextBox txt1 = new TextBox();//textbox
= namevalue[m];//text
= "t" + m;//id
(txt1); add txt1 to the column (txt1);
(td1);//Add column td1 in the row
(td2);//Add column td1 in the row
(tr1);//Add rows in the table
Method to obtain data in textbox:
((TextBox)([m].FindControl("t" + m))).Text Line m id The value of the control whose id is "t" + m
Adding html controls can be used ("")
Add client event method to server-side controls;
For example:["onclick"] = "javascript:alert('shijain')";