做一个类似于答题后台,一个题目多个选择!
也就是点击添加答案时.无刷新添加个TEXTBOX... 
答案是不限制多少个的!...
然后应该怎么获得上面所有输入的值!
帮帮忙谢谢!!最好有例子!谢谢
ID 题目id     
TITLE 选项名称  
ID_SY 所属题目id
RIGHT 是否答案 这是数据库....

解决方案 »

  1.   

    无刷新添加个TEXTBOX要用ajax了
      

  2.   

    09 年的一个帖子http://topic.csdn.net/u/20090325/12/4F891768-01AE-4B03-8A68-1F833964BF1E.html
      

  3.   

    我直接用你的..可以添加TEXTBOX了.但是最后我取不到值呢.!?
      

  4.   

    List<string> l = new List<string>();
    ---------------------------------------l 里保存的就是了。
      

  5.   

    List<string> l = new List<string>();
            int count = 0;
            int.TryParse(hidCount.Value, out count);
            for (int i = 0; i < count; i++)
            {
                l.Add(string.IsNullOrEmpty(Request.Form["txt" + i.ToString()]) ? null : Request.Form["txt" + i.ToString()]);
            }
      

  6.   

    OH,My God.来晚了.
    楼主,恭喜你找到答案了.
      

  7.   

    前台一个panel,两个Button,如下:
            <asp:Panel ID="Panel1" runat="server">
            </asp:Panel>    
            <asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
                Text="添加TextBox" />
            <asp:Button ID="Button2" runat="server" onclick="Button2_Click" 
                Text="获取TextBox值" />
    后台C#:
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["no"] = "0";
            }
            else
            {
                for (int i = 1; i <= Convert.ToInt16(ViewState["no"]); i++)
                {
                    TextBox tb = new TextBox();
                    tb.ID = "tb" + i.ToString();
                    this.Panel1.Controls.Add(tb);
                }
            }
        }
        protected void Button1_Click(object sender, EventArgs e)//添加TextBox
        {
            TextBox tb = new TextBox();
            ViewState["no"] = (Convert.ToInt16(ViewState["no"]) + 1).ToString();
            tb.ID = "tb" + ViewState["no"].ToString();
            this.Panel1.Controls.Add(tb);
        }
        protected void Button2_Click(object sender, EventArgs e)//获取TextBox中的值
        {
            for (int i = 1; i <= Convert.ToInt16(ViewState["no"]); i++)
            {
                string v = (this.Panel1.FindControl("tb" + i.ToString()) as TextBox).Text;
                Response.Write(v + "<br />");
            }
        }
      

  8.   

    l.Add(string.IsNullOrEmpty(Request.Form["txt" + i.ToString()]) ? null : Request.Form["txt" + i.ToString()]);=>
    l.Add(Request.Form["txt" + i.ToString()]);
      

  9.   

    or
    l.Add(Request.Form["txt" + i.ToString()]??"");
      

  10.   

    哦有点看走眼了,要无刷新的!!
    不过放在UpdataPanel里也可以达到无刷新的效果!!
      

  11.   

    还有一个问题想请教下你!
    我想在TEXTBOX后面加个删除..删除多余的TEXTBOX...应该怎么实现呢?
    谢谢
      

  12.   

    sandy945 
    还有一个问题想请教下你!
    我想在TEXTBOX后面加个删除..删除多余的TEXTBOX...应该怎么实现呢?
    谢谢
      

  13.   

    对.我不是点添加TEXTBOX吗?
    但是我添加多了...10个了..
    我现在不想要那么多..我要-去5个..我应该怎么实现?
      

  14.   

    你是希望每个 TextBox 后面都有个删除,还是 只有一个删除
      

  15.   

    搭车问一下,你是怎么解决,页面提交后,让新添加的TEXTBOX还要存在这个问题的?
      

  16.   

    对的..每个Textbox都有一个删除.!
      

  17.   

    回复21楼
    我没有遇到这个问题
    因为我提交后.整个过程就完成了.
    我不需要TEXTBOX还在了!
    这个问题嘛!我帮不了你!
    你问下sandy945他吧!
    他能帮上你!!
    我也是在学习阶段呢!
      

  18.   

    当需求有修改,有时候难免重构。所以选择什么样分析方向也很重要。我在帖子《动态加载用户控件,值怎么存起来?》有一个demo可以参考,但是那个也只是为了那个需求而临时写的demo而已。
      

  19.   

    div.insertAdjacentHTML("beforeEnd","<input type='text' id='txt"+count+"' name='txt"+count+"' /><br>");=>div.insertAdjacentHTML("beforeEnd","<input type='text' id='txt"+count+"' name='txt"+count+"' />&nbsp;&nbsp;<input type='button' value='删除' onclick='del("+count+");' /><br>");
      

  20.   

    至于说所谓的“无刷新”,不外乎把你的TextBox放到UpdatePanel里边,在刷新控件时别忘记执行UpdatePanel的Update方法就行了。
      

  21.   


    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title>无标题页</title>    <script language="javascript" type="text/javascript">
        var count=0;
        function AddTextBox()
        {
            var div = document.getElementById('div');
            //insertAdjacentHTML 不兼容 FF , 如何兼容可查看 prototype ,jQ 等 js 框架。 在这里 insertAdjacentHTML("beforeEnd" 可使用 div.innerHTML+= 替换
            div.insertAdjacentHTML("beforeEnd", "<div id='div" + count + "' ><input type='text' id='txt" + count + "' name='txtName' />&nbsp;&nbsp;<input type='button' value='删除' onclick='del(" + count + ");' /></div>");
            count++;
        }
        function SetValue()
        {
            document.getElementById('<%=hidCount.ClientID %>').value=count;
        }
        function del(id) {
            var div = document.getElementById('div');
            div.removeChild(document.getElementById('div'+id));
        }
        </script></head>
    <body>
        <form id="form1" runat="server">
        <div id="div">
        </div>
        <input type="button" id="btnAdd" value="增加" onclick="AddTextBox()" />
        <asp:Button ID="btnSubmit" runat="server" Text="提交" OnClientClick="SetValue()" OnClick="btnSubmit_Click" />
        <asp:HiddenField ID="hidCount" runat="server" />
        </form>
    </body>
    </html>protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hidCount.Value = "0";
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {                if (!string.IsNullOrEmpty(Request.Form["txtName"]))
            {            int count = 0;
                int.TryParse(hidCount.Value, out count);            string[] myArray = Request.Form["txtName"].Split(',');            StringBuilder sb = new StringBuilder("<script>document.getElementById('div').innerHTML=\"");
                
                for (int i = 0; i < myArray.Length; i++)
                {
                    sb.AppendFormat("<div id='div{0}'><input type='text' id='txt{0}' name='txtName' value='{1}' />&nbsp;&nbsp;<input type='button' id='btn{0}' value='删除' onclick='del({0});' /></div>", i, myArray[i]);
                }
                sb.AppendFormat("\";count={0};</script>", hidCount.Value);            ClientScript.RegisterStartupScript(GetType(), "AddTextBox", sb.ToString());        }
        }
      

  22.   

    用Jquery来进行,添加和选择答案,也就是让他完全在客户端上进行,然后当页面提交的时候用C#来获取值
      

  23.   


    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title>无标题页</title>    <script language="javascript" type="text/javascript">
            var count = <%= txtCount %>;
            
            function AddTextBox() {
                var div = document.getElementById('div');            var html = "<div id='div" + count + "' ><input type='text' id='txt" + count + "' name='txtName' />&nbsp;&nbsp;<input type='button' value='删除' onclick='del(" + count + ");' /></div>";            //insertAdjacentHTML 不兼容 FF , 如何兼容可查看 prototype ,jQ 等 js 框架。 在这里 insertAdjacentHTML("beforeEnd" 可使用 div.innerHTML+= 替换
                div.insertAdjacentHTML("beforeEnd", html);
                count++;
            }
            function SetValue() {
                document.getElementById('<%=hidCount.ClientID %>').value = count;
            }
            function del(id) {
                var div = document.getElementById('div');
                div.removeChild(document.getElementById('div' + id));
            }
        </script></head>
    <body>
        <form id="form1" runat="server">
        <div id="div"><%=divContent %>
        </div>
        <input type="button" id="btnAdd" value="增加" onclick="AddTextBox()" />
        <asp:Button ID="btnSubmit" runat="server" Text="提交" OnClientClick="SetValue()" OnClick="btnSubmit_Click" />
        <asp:HiddenField ID="hidCount" runat="server" />
        </form>
    </body>
    </html>
        protected string divContent, txtCount = "0";        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hidCount.Value = "0";
            }
        }    protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (Request.Form["txtName"] != null)
            {            int count = 0;
                int.TryParse(hidCount.Value, out count);            string[] myArray = Request.Form["txtName"].Split(',');            StringBuilder sb = new StringBuilder();            for (int i = 0; i < myArray.Length; i++)
                {
                    sb.AppendFormat("<div id='div{0}'><input type='text' id='txt{0}' name='txtName' value='{1}' />&nbsp;&nbsp;<input type='button' id='btn{0}' value='删除' onclick='del({0});' /></div>", i, myArray[i]);
                }            divContent = sb.ToString();            txtCount = hidCount.Value;
            }
        }