代码之家  ›  专栏  ›  技术社区  ›  aconstancio

从动态添加的文本框asp.net c中获取值#

  •  7
  • aconstancio  · 技术社区  · 10 年前

    正如标题中所建议的,我可以在其中插入要添加到占位符中的文本框。我可以很好地添加文本框,问题是我无法在这些动态添加的文本框中插入值。这是我的密码

    这段代码的目的是每当文本框出现时,我都可以在其中引入我想要的文本框数量。它创建并将它们添加到我的页面中的占位符。

    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);
        }
    }
    

    这是将提交和响应的按钮的代码。编写插入所有这些文本框中的值。

    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);  
             } 
         }
     }
    

    我一直在网上搜索,我得到的答案是,这段代码很好,应该可以用,但不行。如果你们发现任何问题或有任何建议可以解决我的问题,我会非常感激

    3 回复  |  直到 10 年前
        1
  •  4
  •   Win    10 年前

    你快到了。

    问题

    您需要在回邮时重新加载那些动态创建的文本框。否则,它们将变为空,您将无法找到它。

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

    屏幕截图

    enter image description here

    ASPX公司

    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"/>
    

    代码隐藏

    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;
            }
        }
    }
    
        2
  •  2
  •   Wallstrider    10 年前

    实际上,您正在创建文本框,属性Text设置为默认值=“”;因此,您需要设置txt.Text属性,例如:

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

    编辑:

    之后,您可以将值存储到列表中:

    private static List<string> values = new List<string>();
    
        protected void btnConfirm_Click(object sender, EventArgs e)
        {
            foreach (Control ctr in pholder.Controls)
            {
                if (ctr is TextBox)
                {
                    string value = ((TextBox)ctr).Text;
                    values.Add(value); // add values here
                }
            }
        }
    

    编辑: 以下是您的价值观: enter image description here

    编辑: 为了更好地理解超级巨: 再创建一个文本框 txtOutput 然后添加按钮 GetDataFromTextBoxesAndPutItBelow 并为该按钮“Click”创建一个事件。事件代码:

        protected void btnGetData_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < values.Count; i++)
                txtOutput.Text += "Value from txtquestion1: " + values[i] + " ";
        }
    

    屏幕截图外观: screen2

        3
  •  0
  •   sorak Xavier S.    6 年前
    for (int i = 0; i < dataTable.Rows.Count; i++)
    {
    
        int comment_id = Convert.ToInt32(dataTable.Rows[i]["comment_id"]);
        string created_by_name = dataTable.Rows[i]["created_by_name"].ToString();
        string created_at = dataTable.Rows[i]["created_at"].ToString();
        string comment = dataTable.Rows[i]["comment"].ToString();
    
        HtmlGenericControl divComment = new HtmlGenericControl("div"); //This is root object of comment.Other objects like textbox,button,etc added into this object.
        //divComment.Attributes.Add("class", "div_post_display");
        divComment.Attributes.Add("id", comment_id.ToString());
    
        /* Comment by */
        HtmlGenericControl lblCommentBy = new HtmlGenericControl("label");
        //lblCommentBy.Attributes.Add("class", "divauthor");
        lblCommentBy.InnerText = "" + created_by_name + " (" + created_at + ")";
    
        /* Comment body */
        HtmlGenericControl pComment = new HtmlGenericControl("p");
        //lblCommentBy.Attributes.Add("class", "divauthor");
        pComment.InnerText = comment;
    
        divComment.Controls.Add(lblCommentBy);
        divComment.Controls.Add(pComment);
    
        if (Session["user_id"] != null)
        {
            if (Session["user_level"].ToString() == "1") //Admin can reply for comment
            {
                /* Reply Form */
                TextBox txtReply = new TextBox(); //Create object dynamacaly
                txtReply.ID = "txtReply_"+comment_id;
                txtReply.Attributes.Add("class", "form-control"); //Add css class
                txtReply.Width = 400;
                divComment.Controls.Add(txtReply); //Add obj to root object(div)
    
                Button btnReply = new Button(); //Create object dynamacaly
                btnReply.Text = "Reply"; //Set button text 
                btnReply.Attributes.Add("class", "btn btn-sm btn-success"); //Add css class
                btnReply.Click += btnReply_Click;
                btnReply.CommandArgument = comment_id.ToString();
                divComment.Controls.Add(btnReply); //Add obj to root object(div)
    
                HtmlGenericControl br = new HtmlGenericControl("br"); //Create object dynamacaly
                divComment.Controls.Add(br); //new line
            }
        }
        pnlShowComments.Controls.Add(divComment);
    
    }