C # create a control in the background and obtain the value,
Front-end code
<Form id = "form1" runat = "server"> <div class = "item"> Please input a number: <asp: textBox runat = "server" CssClass = "item" ID = "txtTextCount"> </asp: TextBox> <asp: button runat = "server" ID = "btnCreate" Text = "Create TextBox List" ValidationGroup = "CreateTextBox" OnClick = "btnCreate_Click"/> & nbsp; <asp: button runat = "server" ID = "btnOK" Text = "Get control value" ValidationGroup = "ShowListContent" OnClick = "btnOK_Click"/> </div> <div runat = "server "id =" divControls "class =" item "> </div> <div runat =" server "id =" divMessage "> </div> </form>
Background code
Protected void Page_Load (object sender, EventArgs e) {if (this. isPostBack) {int txtCount = int. parse (txtTextCount. text); // note: the TextBox CreateTextBoxList (txtCount) needs to be dynamically created every time PostBack is performed );}} /// <summary> /// Create textbox list /// </summary> /// <param name = "num"> textbox list count </param> private void CreateTextBoxList (int num) {HtmlGenericControl div; HtmlGenericControl span; TextBox txt; // Regul ArExpressionValidator rev; for (int I = 0; I <num; I ++) {// create div = new HtmlGenericControl (); div. tagName = "div"; div. ID = "divTextBox" + I. toString (); div. attributes ["class"] = "item2"; // create span = new HtmlGenericControl (); span. ID = "spanTextBox" + I. toString (); span. innerHtml = "Url Address" + (I + 1 ). toString () + ":"; // create TextBox txt = new TextBox (); txt. ID = "txt" + I. toString (); tx T. cssClass = "input"; // create a format validation control and associate it with the corresponding TextBox // rev = new RegularExpressionValidator (); // rev. ID = "rev" + I. toString (); // rev. controlToValidate = txt. ID; // rev. display = ValidatorDisplay. dynamic; // rev. validationGroup = "ShowListContent"; // rev. validationExpression = @ "(http (s )? ://)? ([\ W-] + \.) + [\ w-] + (/[\ w -./? % & Amp; =] *)? "; // Rev. ErrorMessage =" Invalid url Address! "; // Add the control to the container div. controls. add (span); div. controls. add (txt); // div. controls. add (rev); divControls. controls. add (div) ;}} protected void btnCreate_Click (object sender, EventArgs e) {txtTextCount. enabled = false; btnCreate. enabled = false;} protected void btnOK_Click (object sender, EventArgs e) {TextBox txt; HtmlGenericControl span; StringBuilder sbResult = new StringBuilder (); int txtCount = int. P Arse (txtTextCount. text); // obtain the Text value in the dynamically created TextBox through traversal (int I = 0; I <txtCount; I ++) {// note: you must use the upper-layer container to obtain the dynamically created TextBox to obtain the ViewState. txt = divControls. findControl ("txt" + I. toString () as TextBox; if (txt! = Null & txt. text. trim (). length> 0) {sbResult. appendFormat ("Url Address {0 }:{ 1 }. <br/> ", I + 1, txt. text. trim () ;}/// retrieve the Text value in the dynamically created TextBox for (int I = 0; I <txtCount; I ++) {// note: you must obtain the dynamically created TextBox through the upper-layer container to obtain the ViewState content span = divControls. findControl ("spanTextBox" + I. toString () as HtmlGenericControl; if (span! = Null & span. innerText. trim (). length> 0) {sbResult. appendFormat ("Url Address {0 }:{ 1 }. <br/> ", I + 1, span. innerText. trim () ;}} divMessage. innerHtml = sbResult. toString ();}