Dynamic addition and deletion of server-side controls in Asp.net

Source: Internet
Author: User

Placeholder: Control container, which is not displayed. It is mainly used to place server controls and only display its child elements (controls in it)

Dynamic addition and deletion of server controls

<1> placeholderDynamically add server-side controls

General methods for adding controls:

Declare a new control such as label lB = new label ()

Set control properties such as LB. Text = "text"

Add the control to placeholder, for example, placeholder1.controls. Add (LB)

You can use viewstate [addedcontrol] = NULL to determine whether it is the first execution.

Add controls dynamically. If you need to bind data, add the control first and then bind the data. After submission (PostBack), you do not need to bind the control again.

Just click New, for example:

If (viewstate [addedcontrol]! = NULL) & (bool) viewstate [addedcontrol]) // do not bind data for the second time
{
Ph1.controls. Clear (); // clear the control in pH1
Dropdownlist DPL = new dropdownlist ();
DPL. ID = "controlid ";
DPL. autopostback = true;
Ph1.controls. Add (DPL );
}
Else
{
Ph1.controls. Clear ();
Dropdownlist DPL = new dropdownlist ();
DPL. ID = "controlid ";
DPL. autopostback = true;
Ph1.controls. Add (DPL );
// Connect to the database and set the data source
DPL. databind ();
Viewstate [addedcontrol] = true;
}

<2> dynamically add server controls to a panel
Add a DataGrid to a panel and bind data

Panel Panel1 = new Panel ();
Panel1.style ["TOP"] = "200px"; // set attributes
This. Controls. Add (Panel1 );
DataGrid DG1 = new DataGrid (); // defines the DataGrid
Boundcolumn S1 = new boundcolumn (); // defines a column.
S1.datafield = "first"; // data source bound to the column
S1.headertext = "MZI"; // set attributes such as the title of this column
Dg1.columns. Add (S1); // Add this column

Dg1.backcolor = "#00000 ";
Dg1.cellpadding = 3;
// Set DG1 attributes
Panel1.controls. Add (DG1 );
// Bind the following data

<3> Add events to dynamically added controls:
For example, add an event to a button.
// Add button 1
Button Bt1 = new button ();
Bt1.commandargument = "Bt1"; // set Command Parameters
Bt1.text = "Daji ";
Bt1.command + = new commandeventhandler (this. onbutton); // scheduled event
Ph1.controls. Add (Bt1 );
// Add button 2, another method
Control cs = parsecontrol ("<asp: button id = 'button2' runat = 'server' text = button 'commandname = 'btn 'commandargument = 'bt2'/> "); // convert a string to a control
Ph1.controls. Add (CS );
Button bt2 = (button) page. findcontrol ("button2 ");
Bt2.command + = new commandeventhandler (this. onbutton); // Add an event to bt2

}
Public void onbutton (Object sender, commandeventargs E)
{
Lab1.text = "label1 ";
}
In this way, both buttons correspond to a function and execute the same event.

If you want them to execute different events,

You can write as follows:
Public void onbutton (Object sender, commandeventargs E)
{
Switch (E. commandargument. tostring (). tolower () to obtain the command parameters. Different commands are executed based on different parameters.
{
Case "Bt1 ";
Lab1.text = "label1 ";
Break;
Case "bt2 ";
Lab1.text = "label2 ";
Break;
}
}

<4>: Add row and server controls to the table

Add a row and two columns in the table to convert the table to the server-side control.
The Code is as follows:

Htmltablerow tr1 = new htmltablerow (); // define a row
Htmltablecell TD1 = new htmltablecell (); // defines the column
Label lB1 = new label (); // defines lB1 as a label Control
Lb1.text = TXT [m]; // lB1
Td1.controls. Add (lB1); // Add LBL to the column
Htmltablecell td2 = new htmltablecell (); // defines the column
Textbox txt1 = new Textbox (); // textbox
Txt1.text = namevalue [m]; // text
Txt1.id = "T" + m; // ID
Td2.controls. Add (txt1); add txt1 to the column
Tr1.cells. Add (TD1); // Add the column TD1 to the row
Tr1.cells. Add (td2); // Add the column TD1 to the row
Table1.rows. Add (tr1); // Add rows in the table

How to obtain data in textbox:

(Textbox) (table1.rows [M]. findcontrol ("T" + M). Text the value of the control whose row ID is "T" + m

Add an HTML control. You can use response. Write ("")

Add client event methods for server controls;
For example, buttton. attributes ["onclick"] = "javascript: Alert ('shijain ')";

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.