0

I create a DIV that runs on the server and then dynamically create and add TextBox controls to that DIV. Later when I try to read the Text property of each TextBox in DIV I cannot get the controls...

How do I accomplish this?

DIV that runs at server...

<div id="divBonusPercents" runat="server"></div>

Dynamically Add Textboxes based on some condition...

 foreach (ListItem item in lbTeamsEOM.Items)
                        if (item.Selected)
                        {
                            // add controls for team bonus %
                            TextBox tbPercent = new TextBox();
                            tbPercent.Text = "15%";
                            tbPercent.ID = "tbPercent" + item.Value;
                            divBonusPercents.Controls.Add(tbPercent);
                        }     

Get the value for each TextBox in the DIV

List<string> lBonusPercent = new List<string>();
            foreach (Control c in divBonusPercents.Controls)
                if (c.GetType() == typeof(TextBox))
                    lBonusPercent.Add(((TextBox)c).Text);   
ѺȐeallү
  • 2,887
  • 3
  • 22
  • 34
  • 1
    At which point do you add the textboxes? During the initial PageLoad or later? – simon at rcl Apr 09 '15 at 16:10
  • later on nextwizardstep – ѺȐeallү Apr 09 '15 at 16:15
  • 1
    Then I don't think this will work, although I'm not sure about when NextWizardStep occurs (never done a wizard in asp.net). Controls created after PageLoad are very tricky due to asp.net Page Life Cycle requirements. Better approach would be to ctrate all the ones you might want at design time, and make them invisible. Then make them visible as required. – simon at rcl Apr 09 '15 at 16:18
  • So my only option is to bloat the DOM with like 100 textboxes when I might only need 3-6 for 95% of the time? – ѺȐeallү Apr 09 '15 at 16:23
  • 1
    Not at all. You could do it in javascript with a postback API. That's how I would. You could create TextBox01 to TextBox10 and use some state to work out what they mean in context (probably not a good idea). There are many things you could do. You could also use a separate page for each step of the Wizard. Options continue... – simon at rcl Apr 09 '15 at 16:26

1 Answers1

0

Previous posters are correct in that you won't be able to access the Text property of the TextBoxes unless they're created early enough in the page life cycle.

However, the actual posted Form elements should still be there in the HTTP Request. Request.Form[**TextBoxObjectHere**.UniqueID] should return whatever text was in a textbox on form post.

Ryan Nigro
  • 4,389
  • 2
  • 17
  • 23