ASP.NET动态添加TextBox控件

来源:互联网 发布:淘宝品牌排行榜 编辑:程序博客网 时间:2024/05/16 14:50

转自:http://blog.csdn.net/keepitshortandsimple/article/details/6177025

方法一:

 

前台代码ASPX:

  1. <form id="form1" runat="server">  
  2.   请输入数量:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>  
  3.   <asp:Button ID="Button1"  
  4.       runat="server" Text="确定" onclick="Button1_Click1" />  
  5.  <div id="divControl" runat="server">  
  6.       
  7.  </div>  
  8.      <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="提交所有信息" />  
  9.     
  10.   </form>  

后台代码ASPX.CS:

  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Linq;   
  4. using System.Web;   
  5. using System.Web.UI;   
  6. using System.Web.UI.WebControls;   
  7.   
  8. public partial class Default2 : System.Web.UI.Page   
  9. {   
  10.     protected void Page_Load(object sender, EventArgs e)   
  11.     {   
  12.   
  13.     }   
  14.   
  15.     /// <summary>   
  16.     /// 首先先循环出你所输入数字的文本框   
  17.     /// </summary>   
  18.     /// <param name="sender"></param>   
  19.     /// <param name="e"></param>   
  20.     protected void Button1_Click1(object sender, EventArgs e)   
  21.     {   
  22.         AddTextBox();   
  23.     }   
  24.   
  25.     //动态添加TextBox   
  26.     private void AddTextBox()   
  27.     {   
  28.         for (int i = 0; i < Convert.ToInt32(TextBox1.Text); i++)   
  29.         {   
  30.             Label li = new Label();   
  31.             li.Text = (i + 1) + ".用户名:";   
  32.             TextBox t = new TextBox();   
  33.             t.ID = "txt" + i.ToString();   
  34.             TextBoxBinds(t, i);   
  35.             divControl.Controls.Add(li);   
  36.             divControl.Controls.Add(t);   
  37.             divControl.Controls.Add(new LiteralControl("<br>"));   
  38.         }   
  39.     }   
  40.   
  41.     /// <summary>   
  42.     /// 然后提交提交所有数据   
  43.     /// </summary>   
  44.     /// <param name="sender"></param>   
  45.     /// <param name="e"></param>   
  46.     protected void Button2_Click(object sender, EventArgs e)   
  47.     {   
  48.         AddTextBox();   
  49.         int txtCount = Convert.ToInt32(TextBox1.Text);   
  50.         //进行验证是否有为空的数据!,该验证必须放在重复循环TextBox信息之后,否则将不会显示回传后的TextBox   
  51.         for (int i = 0; i < txtCount; i++)   
  52.         {   
  53.             String txtValue = Request.Form["txt" + i.ToString()];   
  54.             if (!CheckIsNull(txtValue))   
  55.             {   
  56.                 //如果验证有不符合的将不进行下面数据库相关操作.   
  57.                 return;   
  58.             }   
  59.         }   
  60.   
  61.   
  62.         for (int i = 0; i < txtCount; i++)   
  63.         {   
  64.             Response.Write(Request.Form["txt" + i.ToString()] + "<br>");   
  65.             //Response.Write("现在可以对数据库中的数据循环操作了!");   
  66.         }   
  67.     }   
  68.   
  69.   
  70.     /// <summary>   
  71.     /// 重复提交时对数据的绑定   
  72.     /// </summary>   
  73.     /// <param name="t"></param>   
  74.     /// <param name="i"></param>   
  75.     private void TextBoxBinds(TextBox t, int i)   
  76.     {   
  77.         //通过TextBox的name得到它的值   
  78.         string txtValue = Request.Form["txt" + i.ToString()];   
  79.         //判断该值是否为空   
  80.         if (!String.IsNullOrEmpty(txtValue))   
  81.         {   
  82.             //不为空则对该文本框的值赋值   
  83.             t.Text = txtValue;   
  84.         }   
  85.   
  86.     }   
  87.   
  88.     /// <summary>   
  89.     /// 验证文本框是否为空!   
  90.     /// </summary>   
  91.     /// <param name="txtValue"></param>   
  92.     /// <returns></returns>   
  93.     private bool CheckIsNull(String txtValue)   
  94.     {   
  95.         if (String.IsNullOrEmpty(txtValue))   
  96.         {   
  97.             LiteralControl lc = new LiteralControl();   
  98.             lc.Text = "<script>alert(/"请输入文本框信息!/");</script>";   
  99.             Page.Controls.Add(lc);   
  100.             return false;   
  101.         }   
  102.         return true;   
  103.     }   
  104. }  

 

 

方法二:

 

前台使用js添加Input type="text"html控件:

  1. <!--以下html内容可以用js动态输入-->  
  2. <table>  
  3. <tr>  
  4. <td>  
  5. 姓名</td><td>  
  6. 性别</td><td>身份证   
  7. </td></tr>  
  8. <tr>  
  9. <td>  
  10. <input type="text" name="txtName" /></td><td>  
  11. <select name="lstSex"></select></td><td>  
  12. <input type="text" name="txtCerID" />  
  13. </td></tr>  
  14. <tr>  
  15. <td>  
  16. <input type="text" name="txtName" /></td><td>  
  17. <select name="lstSex"></select></td><td>  
  18. <input type="text" name="txtCerID" />  
  19. </td></tr>  
  20. </table> 

后台代码:

  1. string[] arrName = Request.Form.GetValues("txtName");   
  2. string[] arrSex = Request.Form.GetValues("lstSex");   
  3. string[] arrCerId = Request.Form.GetValues("txtCerID");   
  4. for(int i=0;i<arrName.length;i++){   
  5.     // 取得第i行的数据,后来你就保存去吧   
  6.     string name = arrName[i];   
  7.     string sex = arrSex[i];   
  8.     string cerId = arrCerId[i];   
  9. }   

 

  1. 还可以用split方法来截取值:   
  2. String[] strSprit = Request.Form["txtUsername"].Split(',');   
  3.  for (int i = 0; i < strSprit.Length; i++)   
  4.  {   
  5.      Response.Write(strSprit[i]+"<br>");   
  6.  }  
原创粉丝点击