How to get the dynamically created Textbox value?

Asked

Viewed 3,702 times

2

I have a form that receives an amount of Textbox that must be instantiated, the page then generates the textboxes, but I do not know how to take the values.

hd = Request.QueryString["qtHD"];
mem = Request.QueryString["qtMem"];
hdnum = Convert.ToInt16(hd);

while (hdnum >= 1)
{
    text1 = new TextBox();
    String txtBox = "txtTamHD" + hdnum.ToString();
    text1.ID = txtBox;
    form1.Controls.Add(text1);
    hdnum--;
}
  • Can you please put an example code?

  • Don’t know how to take what values?

  • Using this code snippet to create the Textbox, I wanted to get the values the user typed into them. hd = Request.QueryString["qtHD"]; 
mem = Request.QueryString["qtMem"];
hdnum = Convert.ToInt16(hd); 
 while (hdnum >= 1)
 {
 text1 = new TextBox();
 String txtBox = "txtTamHD" + hdnum.ToString();
 text1.ID = txtBox;
 form1.Controls.Add(text1);
 hdnum--;
 }

  • The data submitted by the controls will probably not be in query-string form. Try using Request.Params["qtHD"].

  • this is the code is what receives the number and instance the textbox, I’m taking the parameters via GET and is working normally with Querystring, my problem is how to get the data of the controls I’m creating now.

  • @rafaslide do not forget to mark as correct the answer that helped you the most. You can also add your answer if none of the others has been satisfactory.

Show 1 more comment

3 answers

2

As @Miguelangelo commented, the catch is on page life cycle. Dynamic controls should be recreated in the Page_Init so that they exist before of the loading of viewstate. This means that, somehow, you must save how many text boxes you created to recreate them again, using the same ID.

Suggestion:

  1. Create an attribute of the type List<TextBox> (let’s call it CamposDinamicos)

  2. Write a method that gets everything it needs to create the text boxes. He should not access anything outside of it, just get his arguments and create the TextBoxes in the CamposDinamicos. The minimum he should receive is the amount of fields.

  3. Call this method on Page_Init. The arguments for it perhaps should be retrieved from Session.

  4. When you need to access the value of dynamic fields, look for them in the list CamposDinamicos and not in FindControls()

This is a translated version of the original reply I posted in https://stackoverflow.com/questions/11992311/get-text-from-dynamically-created-textbox-in-asp-net/11992384#11992384.

  • 1

    +1 ai for the explanation, and for mentioning the view-state... I had even forgotten about it, now that I use MVC for a long time.

1


In Webforms, for dynamically created control values to be retrieved, you must ensure that all controls are created and added to the page hierarchy prior to the Processpostdata event.

After this event in the lifecycle of the page, you should no longer change the hierarchy of page controls, at least in what it says regards controls that post data.

On google images has a good references about the page life cycle... in fact, it was all this complexity that motivated me to switch to ASP.NET MVC.

EDIT 2 Page life cycle image

Ciclo de vida da página http://www.nullskull.com/articles/20051227.asp

EDIT 3

To intercept the page’s Init event, go to the page’s code and do so:

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);

    // ... seu código de criação dos controles dinâmicos aqui
}
  • I’m new in this area, I program only 6 months, for me what Oce said is half Greek, how could I add control to the page hierarchy? I don’t know how to do something before the Loadpostdata event, I heard very well from ASP.NET MVC. I’m looking to study little by little

  • Adding a control to the page hierarchy means adding control (in this case Textbox) anywhere on the page using Controls.Add.

  • I was wrong, the name of the event is Processpostdata.

  • What I mean by before Processpostdata, is to create the dynamic Textbox controls in any of the events of the page that come before it, for example: Oninit, Onpreinit and others... see the figures of the google images I mentioned, so you will have an idea of the order of the events.

  • Reviewing the image now (I edited the response and posted the image), you have to create the controls and add on the page or within any other control until the event OnInitComplete.

0

Opa!

So..

You can make a foreach of Controls.. Like this: foreach (Control controle in this.Controls) { } Then you can sweep all the controls of your form. Makes a if (controle.GetType() == typeof(TextBox)) to see if it’s a Textbox and it’s gone! ;)

Helped?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.