我现在想实现在页面上点一下按钮就生成一个TextBox空间(可生成N个) 如何控制他们的ID?

解决方案 »

  1.   

    可以用js生成input type='text',设置了name属性后,可以在提交时获得值
      

  2.   

    var tb = new TextBox();
    container.Controls.Add(tb);
      

  3.   

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="dynamicAdd.aspx.cs" Inherits="WebApp.dynamicAdd" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>    <script type="text/javascript">
            function addFile() {
                var div = document.createElement("div");
                var f = document.createElement("input");
                f.setAttribute("type", "text")
                f.setAttribute("name", "txtCardNo")
                f.setAttribute("size", "20")
                div.appendChild(f)
                var d = document.createElement("input");
                d.setAttribute("type", "button")
                d.setAttribute("onclick", "deteFile(this)");
                d.setAttribute("value", "移除")
                div.appendChild(d)
                document.getElementById("_container").appendChild(div);
            }        function deteFile(o) {
                while (o.tagName != "DIV") o = o.parentNode;
                o.parentNode.removeChild(o);
            }
        </script></head>
    <body>
        <form id="form1" runat="server">
        <input value="add" type="button" onclick="addFile()" />
        <div id="_container">
        </div>
        <input type="submit" value="ok" />
        </form>
    </body>
    </html>
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;namespace WebApp {
        public partial class dynamicAdd : System.Web.UI.Page {
            protected void Page_Load(object sender, EventArgs e) {
                if (!IsPostBack) {
                    if (string.IsNullOrEmpty(Request["txtCardNo"])) {
                        string[] strCardes = Request["txtCardNo"].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                        foreach (string strCard in strCardes) {
                            //to do
                        }
                    }
                }
            }
        }
    }
      

  4.   

    用js创建
    然后用request.form接收
      

  5.   

    生成客户端的,还是服务器端的呢?客户端的就好办多了.直接接接string串输出就行了.
      

  6.   


    在page_Lode会报错 - -  
      

  7.   

    如果用服务器控件 那就在服务端new TextBox 就行了用html 控件 就用js 或jquery 动态生成html标记和设置id就行了。
      

  8.   

    给控件设置个name属性,后台获取用string name=Request["ControlName"]