Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

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

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.

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.

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top