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

正如我所拥有的标题所示,我可以在其中插入我想要添加到占位符的文本框的数量。 我可以添加文本框就好了问题是我无法在动态添加的文本框中插入值。 这是我的代码

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

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

我一直在网上搜索,我一直在得到这个代码很好的答案,它应该工作,但它没有。 如果你们看到任何错误或有任何可以解决我的问题的建议,我真的很感激

你快到了。

问题

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

为此,您需要将动态TextBoxes ID保存在持久位置,例如View State或Session State。

屏幕截图

在此处输入图像描述

ASPX

 Number of TextBoxes: 


Result:

代码背后

 private List TextBoxIdCollection { get { var collection = ViewState["TextBoxIdCollection"] as List; return collection ?? new List(); } 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(); 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; } } } 

您实际上是创建文本框,其属性Text设置为default =“”; 所以你需要设置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 values = new List(); 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 } } } 

编辑:这是你的价值观: 在此处输入图像描述

编辑:为了超级更好的理解:再创建一个文本框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] + " "; } 

截图看起来: 画面2

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