• <tfoot id='VlMyf'></tfoot>
    1. <legend id='VlMyf'><style id='VlMyf'><dir id='VlMyf'><q id='VlMyf'></q></dir></style></legend>
      <i id='VlMyf'><tr id='VlMyf'><dt id='VlMyf'><q id='VlMyf'><span id='VlMyf'><b id='VlMyf'><form id='VlMyf'><ins id='VlMyf'></ins><ul id='VlMyf'></ul><sub id='VlMyf'></sub></form><legend id='VlMyf'></legend><bdo id='VlMyf'><pre id='VlMyf'><center id='VlMyf'></center></pre></bdo></b><th id='VlMyf'></th></span></q></dt></tr></i><div id='VlMyf'><tfoot id='VlMyf'></tfoot><dl id='VlMyf'><fieldset id='VlMyf'></fieldset></dl></div>

      1. <small id='VlMyf'></small><noframes id='VlMyf'>

          <bdo id='VlMyf'></bdo><ul id='VlMyf'></ul>

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

        时间:2023-06-09

          <i id='8XbEw'><tr id='8XbEw'><dt id='8XbEw'><q id='8XbEw'><span id='8XbEw'><b id='8XbEw'><form id='8XbEw'><ins id='8XbEw'></ins><ul id='8XbEw'></ul><sub id='8XbEw'></sub></form><legend id='8XbEw'></legend><bdo id='8XbEw'><pre id='8XbEw'><center id='8XbEw'></center></pre></bdo></b><th id='8XbEw'></th></span></q></dt></tr></i><div id='8XbEw'><tfoot id='8XbEw'></tfoot><dl id='8XbEw'><fieldset id='8XbEw'></fieldset></dl></div>
          • <small id='8XbEw'></small><noframes id='8XbEw'>

            <tfoot id='8XbEw'></tfoot>

              <tbody id='8XbEw'></tbody>

                <bdo id='8XbEw'></bdo><ul id='8XbEw'></ul>
                1. <legend id='8XbEw'><style id='8XbEw'><dir id='8XbEw'><q id='8XbEw'></q></dir></style></legend>
                  本文介绍了从动态添加的文本框中获取值 asp.net c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

                  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

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

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

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

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

                  我一直在网上搜索,我得到的答案是这段代码很好,它应该可以工作,但它没有.如果你们发现任何问题或有任何可以解决我问题的建议,我将不胜感激

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

                  代码隐藏

                  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#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:WPF 文本框的验证规则 下一篇:如何在winform中允许带有TextBox的ctrl + a?

                  相关文章

                      <i id='XNvIR'><tr id='XNvIR'><dt id='XNvIR'><q id='XNvIR'><span id='XNvIR'><b id='XNvIR'><form id='XNvIR'><ins id='XNvIR'></ins><ul id='XNvIR'></ul><sub id='XNvIR'></sub></form><legend id='XNvIR'></legend><bdo id='XNvIR'><pre id='XNvIR'><center id='XNvIR'></center></pre></bdo></b><th id='XNvIR'></th></span></q></dt></tr></i><div id='XNvIR'><tfoot id='XNvIR'></tfoot><dl id='XNvIR'><fieldset id='XNvIR'></fieldset></dl></div>
                    1. <tfoot id='XNvIR'></tfoot>

                      <small id='XNvIR'></small><noframes id='XNvIR'>

                        <bdo id='XNvIR'></bdo><ul id='XNvIR'></ul>

                    2. <legend id='XNvIR'><style id='XNvIR'><dir id='XNvIR'><q id='XNvIR'></q></dir></style></legend>