textbox text property empty when added at runtime

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cyrak
    New Member
    • Mar 2008
    • 9

    textbox text property empty when added at runtime

    This should be fairly trivial I think..but is not working.
    I have a textbox that i add at runtime

    TextBox tagText = new TextBox();

    then i put it in a panel'

    PanelWODetails. Controls.Add(ta gText);

    once the user enters some text and click on a button I try to grab that text, but the Text property is always empty.

    any ideas why this would happen?
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    let's see the code where you are trying to get the text; maybe the issue is there

    Comment

    • cyrak
      New Member
      • Mar 2008
      • 9

      #3
      ok..so what i do to get it is
      I save all the textboxes in a collection so that I don't have to find them in the controls collection ( I assume this shouldn't be a problem given C# only uses references...ma ybe this is bad assumption(?) ) and then i just do
      Code:
      //words is a regular string array
      words[i] = ((TextBox)TBoxes[boxNumber]).Text;
      edit:
      ///ok so this is kind of weird to me... right after I finish creating the textboxes i make a copy of the panel's control collection and put it in a Session variable.
      When Postback happens the panel's controls are gone but the session variable still has all of them and the last line executing is

      Code:
      Session["collection"] = PanelWODetails.Controls;
      and when postback happens the PanelWO.Details controls are gone..there is just a literal there
      Last edited by Frinavale; Apr 9 '09, 06:22 PM. Reason: Added code tags. Please post code in [code] [/code] tags.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        You have to instantiate your TextBox in the Page Init Event.
        Please see the article about how to use dynamic controls in ASP.NET for more information on why. (Sorry that the code is in VB.NET but the concepts in that article are important to grasp)

        -Frinny

        Comment

        • cyrak
          New Member
          • Mar 2008
          • 9

          #5
          thank you! that explains the why..
          Now your article only adds up to 6 textboxes
          my webapp parses text from a selectedCell in a gridview and when it finds a certain word it adds a textbox to a panel...there could be 2 or 70 I don't find it very wise to instantiate a whole bunch of textboxes when i might be only using 2 and i also need to have some labels in between those textboxes...is it really the only way?

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Hmmm, you don't have to use an Array to store the TextBoxes and Lables, that was just for simplicity sake for the article.

            You could try storing them in a generic List(T) so that the list can grow to the size you require...

            You'll need to store the number of TextBoxes and Labels somewhere so that you know how many to instantiate during the Page Init event.

            I would recommend storing them in a cookie, HiddenField, or as parameters in the URL so that you can grab these values from the Request Object. Remember that you can't store this value in ViewState because it's not available at this stage in the page's life cycle.

            Comment

            Working...