得到动态添加文本框asp.net C#值 [英] get values from dynamically added textboxes asp.net c#

查看:260
本文介绍了得到动态添加文本框asp.net C#值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所建议的标题我有,我可以插入我想多少文本框添加到一个占位符。我可以添加文本框就好了,问题是我不能让插入这些动态添加文本框的值。这里是我的code

as suggested in the title i have in which i can insert how many textboxes i want to add to a placeholder. i can add the textboxes just fine the problem is i cant get the values inserted on those dynamically added textboxes. here's my code

这片code的目的是每当文本中,我可以介绍我想文本框的数量。它创建,并将它们添加到我的网页中的占位符。

the purpose of this piece of code is to whenever the textbox in which i can introduce the number of textboxes i want. it creates and adds them to the placeholder in my page.

public void txtExtra_TextChanged(object sender, EventArgs e)
{  
    for (a = 1; a <= int.Parse(txtExtra.Text); a++)
    {
         TextBox txt = new TextBox();
         txt.ID = "txtquestion" + a;
         pholder.Controls.Add(txt);
    }
}

这是该按钮,将提交和RESPONSE.WRITE插在所有这些文本框的值的code。

this is the code of the button that will submit and response.write the values inserted in all those textboxes.

protected void btnConfirm_Click(object sender, EventArgs e)
{
     foreach (Control ctr in pholder.Controls)
     {
         if (ctr is TextBox)
         {        
              string value = ((TextBox)ctr).Text;
              Response.Write(value);  
         } 
     }
 }

我一直在网上搜索和我已经得到的答案,这code是罚款,它应该工作,但它不。如果你们看到什么问题或者有可以解决我的问题的任何建议我真的AP preciate它

i've been searching online and i've been getting answers that this code is fine and it should work but it doesnt. if you guys see anything wrong or have any suggestion that can solve my problem i'd really appreciate it

推荐答案

您几乎没有。

您需要重新加载后的动态创建文本框回来。否则,他们将成为空,你将无法找到它。

You need to reload those dynamically created textboxes on post back. Otherwise, they will become null, and you won't be able to find it.

在为了做到这一点,你需要保存这些动态文本框的ID在持久位置,如视图状态或会话状态。

In order to do that, you need to save those dynamically TextBoxes Ids in persistent location such as View State or Session State.

Number of TextBoxes: <asp:TextBox runat="server" ID="CounterTextBox" 
    OnTextChanged="CounterTextBox_TextChanged" AutoPostBack="True" /><br/>
<asp:PlaceHolder runat="server" ID="TextBoxPlaceHolder" /><br/>
<asp:Button runat="server" ID="ConfirmButton" Text="Confirm" 
    OnClick="ConfirmButton_Click" /><br/>
Result: <asp:Literal runat="server" ID="ResultLiteral"/>

code

的背后

private List<string> TextBoxIdCollection
{
    get
    {
        var collection = ViewState["TextBoxIdCollection"] as List<string>;
        return collection ?? new List<string>();
    }
    set { ViewState["TextBoxIdCollection"] = value; }
}

protected void Page_Load(object sender, EventArgs e)
{
    foreach (string textboxId in TextBoxIdCollection)
    {
        var textbox = new TextBox {ID = textboxId};
        TextBoxPlaceHolder.Controls.Add(textbox);
    }
}

protected void CounterTextBox_TextChanged(object sender, EventArgs e)
{
    var collection = new List<string>();
    int total;
    if (Int32.TryParse(CounterTextBox.Text, out total))
    {
        for (int i = 1; i <= total; i++)
        {
            var textbox = new TextBox { ID = "QuestionTextBox" + i };
            // Collect this textbox id
            collection.Add(textbox.ID); 
            TextBoxPlaceHolder.Controls.Add(textbox);
        }
        TextBoxIdCollection= collection;
    }
}

protected void ConfirmButton_Click(object sender, EventArgs e)
{
    foreach (Control ctr in TextBoxPlaceHolder.Controls)
    {
        if (ctr is TextBox)
        {
            string value = ((TextBox)ctr).Text;
            ResultLiteral.Text += value;
        }
    }
}

这篇关于得到动态添加文本框asp.net C#值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆