Get text from dynamically created textbox in asp.net

后端 未结 3 1441
无人及你
无人及你 2020-12-20 22:07

I\'ve been banging my head against this all morning, so hopefully I can get some help. Essentially I\'m having issues getting values from some textbox controls I\'m creating

相关标签:
3条回答
  • 2020-12-20 22:16

    You add TextBox controls to templateFormPlaceholder.Controls but use form1.FindControl to find them. FindControl method will find a control only if the control is directly contained by the specified container - from http://msdn.microsoft.com/en-us/library/486wc64h.aspx. Try calling templateFormPlaceholder.FindControl instead.

    0 讨论(0)
  • 2020-12-20 22:21

    Create Dynamic TextBoxes and add it to a asp panel so that you can access it easily.

    Here is the ASP.NET design elements.

    <div class="form-group">
    <asp:Panel ID="panel" runat="server" CssClass="form-group">
    
    </asp:Panel>
    </div>
    

    C# Code to generate Dynamic textboxes

     protected void create_dynamic_text(object sender, EventArgs e)
    {
        int num = 5; // you can give the number here
        for (int i = 0; i < num;i++ )
        {
            TextBox tb = new TextBox();
            tb.ID = "txt_box_name" + i.ToString();
            tb.CssClass = "add classes if you need";
            tb.Width = 400; //Manage width and height 
            panel.Controls.Add(tb); //panel is my ASP.Panel object. Look above for the design code of ASP panel
        }
    }
    

    C# Code to Take Values

     protected void reade_values(object sender, EventArgs e)
    {
       int num=5; // your count goes here
        TextBox tb = new TextBox();
            for (int i = 0; i < num; i++)
            {
               tb=(TextBox)panel.FindControl("txt_box_name"+i.ToString());
               string value = tb.Text; //You have the data now
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-20 22:37

    You're right, it's all about the page lifecycle. Dynamically created controls must be re-created at the Page_Init stage, in order to exist before the viewstate binding stage. This means that will have to somehow (using the Session, maybe) store how many textboxes you have created on the previous processing to recreate them. Remind to use the same ID and to add them to your control tree (a repeater or something else that you're using).


    UPDATE

    Let me give you a suggestion: 1. Declare a class attribute of type List<TextBox> (let's call it CreatedTextBoxes)

    1. Declare a method that receives whatever it needs to create the textboxes. This method must not read anything outside of it's scope. It will simply receive some args, create the textboxes and add them to another control (such as a Repeater). Add each textbox created to CreatedTextBoxes

    2. At the dropdown change event, read the option, save it to the Session and call this method

    3. At Page_Init, verify that object at the Session. If it's null or empty, don't do anything. If it has a value, call that same method, passing the same args

    4. When you need to retrieve that from the dynamically created textboxes, use CreatedTextBoxes and not FindControls()
    0 讨论(0)
提交回复
热议问题