0

I wonder if someone can help

I have a ASP.NET form and by clicking a button on the page user should be able to add new dynamic controls to it. the button click should be add one dropdown and two textboxes like append to a DIV or to panel.

However then on click of another button I would like to read values of all the three controls on the backend.

How can this be done, thanks in advance.

user1144596
  • 2,068
  • 8
  • 36
  • 56

1 Answers1

0

You can look at the classes in the System.Web.UI.WebControls Namespace to get a full list of options for dynamically creating and reading controls in .net / c#. Here's a quick example from MS that covers the how-to.

To read the controls you create, make sure you create them with a strong ID or class such as "CustomerFirstName" or "Address2" so that you can access them easily. You also have control over the parent so you can access a parent control and then loop through each child and access data in each

Graham
  • 336
  • 1
  • 13
  • the controls are lost in a postback event, any way to retain them. – user1144596 Apr 09 '15 at 07:39
  • The easiest way I can think of is to have a function called when the submit/post button is clicked that will read the controls on the page (either by reading by class or by storing the ID in an array for the session and querying those back) and then at the end of that function (and after validation/error handling) triggering the post from the code-behind rather than the asp form. – Graham Apr 09 '15 at 15:57
  • If you're using code behind, functions called from things like a button click will allow you to work with the code as soon as the function is called. .Net should take care of the how part of it, you just have to call the function and then work with the data. You can then choose to route it to a redirect to another form, a field update, a database push, etc. [Here's](http://stackoverflow.com/questions/5381139/linkbutton-send-value-to-code-behind-onclick) a StackOverflow article on how to use a link button to call a function in code behind. – Graham Apr 09 '15 at 20:03