Asp. NET Dynamic add TextBox control __.net

Source: Internet
Author: User

Method One:

Foreground code aspx: <form id= "Form1" runat= "Server" > Please enter a quantity: <asp:textbox id= "TextBox1" runat= "Server" ></asp: textbox> <asp:button id= "Button1" runat= "Server" text= "OK" onclick= "Button1_click1"/> <div id= "Divcontrol" runat= "Server" > </div> <asp:button id= "Button2" runat= "Server" onclick= "button2_ Click "text=" submit all information "/> </form>

Background Code ASPX.CS:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls; PublicPartialclassDefault2:System.Web.UI.Page {protectedvoidPage_Load (Objectsender, EventArgs e) {}///<summary>///first loop through the text box of the number you entered///</summar y>///<param name= "sender" ></param>///<param name= "E" ></param>protectedvoidButton1_click1 (Objectsender, EventArgs e) {AddTextbox (); }//Add TextBox dynamicallyPrivatevoidAddTextbox () { for(inti = 0; I < Convert.ToInt32 (TextBox1.Text); i++) {Label Li =NewLabel (); Li.                Text = (i + 1) + ". Username:"; TextBox T =NewTextBox ();                t.id = "txt" + i.tostring ();                Textboxbinds (t, i);                DivControl.Controls.Add (LI);                DIVCONTROL.CONTROLS.ADD (t); DIVCONTROL.CONTROLS.ADD (NewLiteralControl ("<br>")); }///<summary>///then submit all data///</summary>///<param name= "send Er "></param>///<param name=" E "></param>protectedvoidButton2_Click (Objectsender, EventArgs e) {AddTextbox ();intTxtcount = Convert.ToInt32 (TextBox1.Text); Verify that there is null data!, the validation must be placed after repeating circular textbox information, otherwise the returned textbox will not be displayed for(inti = 0; i < Txtcount; i++) {String txtValue = request.form["txt" + i.tostring ()];if(! Checkisnull (TxtValue)) {//if validation has a non conformance will not do the following database-related operations. return; }            } for(inti = 0; i < Txtcount;                i++) {Response.Write (request.form["TXT" + i.tostring ()] + "<br>");            Response.Write ("Now you can loop through the data in the database!"); }///<summary>///bind to data on duplicate commit///</summary>///<param name= " T "></param>///<param name=" I "></param>Private voidTextboxbinds (TextBox T,inti) {//Get its value through the name of the textboxstringTxtValue = request.form["txt" + i.tostring ()]; Determine if the value is emptyif(!            String.IsNullOrEmpty (TxtValue)) {//Is not NULL to assign the value of the text box T.text = TxtValue;        }///<summary>///Verify that the text box is empty! </summary>///<param name= "TxtValue" ></param>///<returns></returns>PrivateBOOLCheckisnull (String txtValue) {if(String.IsNullOrEmpty (TxtValue)) {LiteralControl LC =NewLiteralControl (); Lc.                Text = "<script>alert (/" Please enter text box information!/");</script>"; PAGE.CONTROLS.ADD (LC); returnfalse; } returntrue; }    }

Method Two:

The foreground uses JS to add the input type= "text" HTML control:

<!--The following HTML content can be dynamically entered with JS-->   <table>   <tr>   <td>    name </td><td>   sex </td><td> ID    </td></tr>    <tr>   <td>   <input type= "text"  name= "Txtname"  /> </td><td>   <select name= "Lstsex" ></select></td><td>    <input type= "text"  name= "Txtcerid"  />   </td></tr>   <tr>   <td>   <input type= "text"  name= "Txtname"  /></td ><td>   <select name= "Lstsex" ></select></td><td>   <input type= "text"  name= "Txtcerid"  />   </td></tr>   </ table> 

Background code: string[] arrname = request.form.getvalues ("txtname");    String[] arrsex  = request.form.getvalues ("Lstsex");    string[] arrcerid = request.form.getvalues ("Txtcerid");    for (int i=0;i<arrname.length;i++) {       //  get data in line I, Then you have to save it        string name = arrName[i];        string sex = arrSex[i];        string cerId = arrCerId[i];   }   

You can also use the Split method to intercept the value: string[] Strsprit = request.form["txtUserName"].     Split (', ');     for (int i = 0; i < strsprit.length i++) {Response.Write (strsprit[i]+ "<br>"); }

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.