Asp.net dynamically generates controls and obtains their values

Source: Internet
Author: User

Copy codeThe Code is as follows:
// Dynamically generate three controls
Protected void button#click (object sender, EventArgs e)
{
For (int I = 0; I <3; I ++)
{
TextBox t = new TextBox ();
T. ID = string. Format ("newTextBox {0}", I );
Panel1.Controls. Add (t );
}
ListControlsInPanel ();
}

Copy codeThe Code is as follows:
// Obtain the value of the dynamically generated control
Protected void Button3_Click (object sender, EventArgs e)
{
String str = "";
String [] ak = Request. Form. AllKeys;
For (int I = 0; I <Request. Form. Count; I ++)
{
// Only the values of the three dynamically generated controls are filtered out.
If (ak [I]. IndexOf ("new")>-1)
Str + = string. Format ("<li> {0} </li> <br>", Request. Form [I]);
}
Label1.Text = str;
}

// The ID of the newly generated control starts with "new". At the same time, ensure that no control ID on the page contains "new.

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.