动态添加TextBox
方法1:
前台代码:
- <form id="form1" runat="server">
- 请输入数量:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
- <asp:Button ID="Button1"
- runat="server" Text="确定" onclick="Button1_Click1" />
- <div id="divControl" runat="server">
- </div>
- <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="提交所有信息" />
- </form>
.aspx.cs代码(后台):
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- public partial class Default2 : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- /// <summary>
- /// 首先先循环出你所输入数字的文本框
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- protected void Button1_Click1(object sender, EventArgs e)
- {
- AddTextBox();
- }
- //动态添加TextBox
- private void AddTextBox()
- {
- for (int i = 0; i < Convert.ToInt32(TextBox1.Text); i++)
- {
- Label li = new Label();
- li.Text = (i + 1) + ".用户名:";
- TextBox t = new TextBox();
- t.ID = "txt" + i.ToString();
- TextBoxBinds(t, i);
- divControl.Controls.Add(li);
- divControl.Controls.Add(t);
- divControl.Controls.Add(new LiteralControl("<br>"));
- }
- }
- /// <summary>
- /// 然后提交提交所有数据
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- protected void Button2_Click(object sender, EventArgs e)
- {
- AddTextBox();
- int txtCount = Convert.ToInt32(TextBox1.Text);
- //进行验证是否有为空的数据!,该验证必须放在重复循环TextBox信息之后,否则将不会显示回传后的TextBox
- for (int i = 0; i < txtCount; i++)
- {
- String txtValue = Request.Form["txt" + i.ToString()];
- if (!CheckIsNull(txtValue))
- {
- //如果验证有不符合的将不进行下面数据库相关操作.
- return;
- }
- }
- for (int i = 0; i < txtCount; i++)
- {
- Response.Write(Request.Form["txt" + i.ToString()] + "<br>");
- //Response.Write("现在可以对数据库中的数据循环操作了!");
- }
- }
- /// <summary>
- /// 重复提交时对数据的绑定
- /// </summary>
- /// <param name="t"></param>
- /// <param name="i"></param>
- private void TextBoxBinds(TextBox t, int i)
- {
- //通过TextBox的name得到它的值
- string txtValue = Request.Form["txt" + i.ToString()];
- //判断该值是否为空
- if (!String.IsNullOrEmpty(txtValue))
- {
- //不为空则对该文本框的值赋值
- t.Text = txtValue;
- }
- }
- /// <summary>
- /// 验证文本框是否为空!
- /// </summary>
- /// <param name="txtValue"></param>
- /// <returns></returns>
- private bool CheckIsNull(String txtValue)
- {
- if (String.IsNullOrEmpty(txtValue))
- {
- LiteralControl lc = new LiteralControl();
- lc.Text = "<script>alert(\"请输入文本框信息!\");</script>";
- Page.Controls.Add(lc);
- return false;
- }
- return true;
- }
- }
方法2:
- <!--以下html内容可以用js动态输入-->
- <table>
- <tr>
- <td>
- 姓名</td><td>
- 性别</td><td>身份证
- </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>
- 后台用:
- 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++){
- // 取得第i行的数据,后来你就保存去吧
- string name = arrName[i];
- string sex = arrSex[i];
- string cerId = arrCerId[i];
- }
- 还可以用split方法来截取值:
- String[] strSprit = Request.Form["txtUsername"].Split(',');
- for (int i = 0; i < strSprit.Length; i++)
- {
- Response.Write(strSprit[i]+"<br>");
- }