views:

2857

answers:

8

Im adding textboxes (not a fixed number of textboxes) dynamically to a form on ASP.NET page, how do i read back data from these textboxes?

+1  A: 

Look at Request.Params and extract them from there. You will, of course, have to give them ids to be able to tell them apart.

tvanfosson
+1  A: 

When you add them you should be giving them names/ids, and you can use those to reference them.

If not, walk your DOM in javascript to find them inside the form you made - they'll be in the same order you inserted them.

Lastly, they're all available as post/get inputs to your page, so you should be able to look at them all as long as you assigned them different names.

Adam Davis
+1  A: 

From all the ASP.NET apps I've worked with, .NET likes to use the following algorithm when generating the Id for server controls:

ctl00$cphBody$[ControlID]

Try using this algorithm when accessing your data from the dynamically generated textboxes.

Dillie-O
+3  A: 

Assuming you're wanting to access the controls on the postback you'd probably re-create the dynamic controls exactly as they were created on the initial load, then use the page's FindControls method to find the controls. It would probably help to create the textboxes with IDs like Textbox1, Textbox2, etc.

marc
A: 

When creating textboxes dynamically (presumably using JavaScript, but same goes for ASP.NET controls) give them names in a specific pattern. The one you will be able to recognize later.

On server-side, in any event handler occurring after Page_Init you can iterate through Request.Form collection.

Do not be tempted to use Request.Param because it can be used to apply cross-site request forgery on your application (an attacker could lure user into issuing a GET request which your application would interpret the same as it would interpret a POST one, which is usually not a good thing).

If you are adding dynamic ASP.NET controls (in Page_Render for example) you can also reconstruct controls and use their properties.

Damir Zekić
Form parameters are no safer than query string parameters in this respect. Anyone who can induce someone to issue a GET can also induce them to issue a POST. You need to use things like encrypted cookies, validation elements, etc. to make sure requests are coming from your pages.
tvanfosson
You are most certainly right, but reading collection that contains all variables where all you want are POST-values is just making the job for a potential attacker easier and does not make any sense to me.
Damir Zekić
A: 

To create dynamic controls, I would usually use a ASP.NET PlaceHolder Control and add the dynamic controls to this container.

I would give each dynamic control an ID.

You can then subsequently use FindControl on the PlaceHolder to access the dynamic controls.

I say "dynamic controls" to mean controls you add at run-time

hmak
this wont solve the problem as you have to add them before the Load event.
pokrate
A: 

You can use FindControl and pass the textbox ID to get an instance of the textbox when post back. The Text property contains the data, given that we are at page load stage or later in the cycle. When adding dynamic controls, override the CreateChildControls method and add the dynamic controls to control hierarchy at this stage of the cycle.

baretta
A: 

Remember that in ASP.Net, every postback is a new instance of your class. If you created these controls during a previous postback or on the first view then they were garbage collected with the rest of that previous instance. So to use the controls in this new instance, you need to create them again. If you need the state information loaded for those controls (including any value entered by the user), you also need to create before the viewstate is loaded, meaning you do it during the Init event, rather than the load event.

Joel Coehoorn