How can I dynamically Add a control to a table and obtain the control value?

Source: Internet
Author: User
The result of this example is:
Each time you press "button1", add a row (two columns in the row, one column is the text box and the other column is the drop-down box) to table 1, and add another button when the button is pressed for the first time, click the Add button to output the values of all controls in the table.

Foreground: <Form ID = "form1" method = "Post" runat = "server">
<Asp: Table id = "Table1" runat = "server"> </ASP: Table>
<Asp: placeholder id = "placeholder1" runat = "server"> </ASP: placeholder> <br>
<Asp: button id = "button1" runat = "server" text = "add a row"> </ASP: button>
</Form>


Place a table to dynamically add controls, place a placeholder to dynamically add buttons, press this button to obtain the control value in the table, and press the button1 button to add a row.

Background:

Button1 button event: Private void button#click (Object sender, system. eventargs E)
{
Addtextboxs ();
If (viewstate ["count"] = NULL) addbutton ();
Viewstate ["count"] = convert. toint16 (viewstate ["count"]) + 1;
}

Two Methods: one is used to dynamically Add rows in the table and the other is used to dynamically add buttons (the button is not added once by pressing button1, So If (viewstate ["count"] = NULL) is added) private void addtextboxs ()
{
Tablerow TR = new tablerow ();
Tablecell TC1 = new tablecell ();
Textbox T = new Textbox ();
T. ID = "TB" + table1.rows. count;
Tc1.controls. Add (t );
Tablecell TC2 = new tablecell ();
Dropdownlist DPL = new dropdownlist ();
DPL. ID = "DPL" + table1.rows. count;
For (INT I = 0; I <10; I ++) DPL. Items. Add (I. tostring ());
Tc2.controls. Add (DPL );
Tr. cells. Add (TC1 );
Tr. cells. Add (TC2 );
Table1.rows. Add (TR );
}

Private void addbutton ()
{
Button B = new button ();
B. ID = "BTN ";
B. Text = "button ";
B. Click + = new system. eventhandler (btn_click );
Placeholder1.controls. Add (B );
}

Finally, the event of the dynamically added button: Private void btn_click (Object sender, system. eventargs E)
{
For (INT I = 0; I <table1.rows. Count; I ++)
{
Response. write (textbox) table1.rows [I]. findcontrol ("TB" + I )). text + (dropdownlist) table1.rows [I]. findcontrol ("DPL" + I )). selectedvalue + "<br> ");
}
}

In fact, the dynamically added controls are not complex. You only need to note that the dynamically added controls also need to be added again during the PostBack operation. How do you know if you have pressed the button, or how do I know how many times I have pressed the button? Store it in viewstate with a flag.

Page_load event: Private void page_load (Object sender, system. eventargs E)
{
If (viewstate ["count"]! = NULL)
{
For (INT I = 0; I <convert. toint16 (viewstate ["count"]); I ++)
Addtextboxs ();
Addbutton ();
}
}

Do not add If (! Ispostback) {}. On the contrary, you can add If (ispostback) because it is impossible to press the button for the first page loading.

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.