2

Hy,

I have created some dynamic textboxes with standard content.

Does anyone know how can I read the content of these textboxes (assuming that user modified the standard content) when I press one button?

Thanks a lot.

Jeff

Update

This is how I am creating the textboxes:

foreach (string name in listOfNames)
{
   TextBox tb = new TextBox();
   tb.Text = name;
   tb.BorderStyle = BorderStyle.None;
   tb.BorderWidth = 0;
   tb.Font.Name = "Arial";
   tb.Font.Size = 8;
}
Jeff Norman
  • 1,014
  • 5
  • 25
  • 42

4 Answers4

3

The specific will vary depending on the technology you are using. However the concept would remain very similar, though for ASP.NET it will be a little more interesting.

WinForms/WPF/Silverlight

Maintain a list of the dynamically created textboxes and when the button is pressed you can run through the list of textboxes and read the Text property to get the user input.

ASP.NET - After the tag update it seems this section is most appropriate to your requirement.

For ASP.NET you will need to create the textboxes in an override of the OnInit method, this should happen on each postback. Then in the Button.Click event you can read the user input from the textboxes that you created in the OnInit function. You need to ensure that the controls are created with the same ID on each post back.

Chris Taylor
  • 52,623
  • 10
  • 78
  • 89
  • +1 - for "Same ID"...without a consistent ID, dynamic controls will render but all sorts of problems occur (like events that only fire when you click the element twice). – Tim M. Dec 21 '10 at 14:53
2

You need to ensure that the text boxes are recreated on every postback.

If you do not recreate them, you will not be able to access their properties or events.

The best place to create dynamic controls is the page Init event handler.

I suggest reading up on the ASP.NET page life cycle.


Update (following updated question)

Make sure to set an ID (and a different one, at that) for the text boxes, so you can refer to them later on.

I can't see where you are adding these controls to the page either.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • @Richard Friend - I never did that, but then again, I could be wrong. Can you point me to any articles that say this? – Oded Dec 21 '10 at 13:59
  • I know creating in Init works, however a lot of controls rely on EnsureChildControls() which inturn looks at ChildControlsCreated which in turn calls CreateChildControls. see http://www.singingeels.com/Articles/Dynamically_Created_Controls_in_ASPNET.aspx – Richard Friend Dec 21 '10 at 14:01
  • @Richard Friend - He also says (almost the very start of the article) that the page Init event handler is the best place. Using `CreateChildControls` is used for addressing a specific issue with exceptions. – Oded Dec 21 '10 at 14:03
  • He does say that but the summary at the end states to use CreateChildControls. again this is an area which seems unclear, however ms states thats what its for http://msdn.microsoft.com/en-us/library/system.web.ui.control.createchildcontrols.aspx – Richard Friend Dec 21 '10 at 14:08
  • http://stackoverflow.com/questions/1504193/when-must-i-use-asp-net-createchildcontrols – Richard Friend Dec 21 '10 at 14:08
0

Request.Form is a collection of Key-Value pairs that represents all of the data coming back from the ASP.NET request. If you access that you can get any value whether its control is specified in the ASPX code or dynamically created in the code behind file.

You can also get their values placed back in them automatically if you recreate them during the init part of the page lifecycle. If you do this, their values will be set when ASP.NET recreates the state of the page and applies the values from the form.

Brendan Enrick
  • 4,277
  • 2
  • 26
  • 40
0

You should be able to access them as you would a normal control, you just need to get a reference to the control. Remember to re-create all controls on each postback.

this is a useful article on dynamic controls in asp.net

Richard Friend
  • 15,800
  • 1
  • 42
  • 60