0

In my page, i got two column and multiple rows. The first column contain the label such as question for the 1st row, and the options for the questions. And the second column is the textboxes. When i click on add button, i wish to add those controls to page which subsequently allowed me to add the value in the texbox to database. I did some research but most of them uses javascript or datatable. Is there any other method?

3
  • If you can at all avoid it don't add controls dynamically in ASP.NET. It's better to set them visible / invisible using Control.Visible Commented Feb 9, 2012 at 1:17
  • But i need to add multiple questions. And i also not sure how many questions can it goes. Maybe 1 or even to 100. So i cannot use the visible method. Commented Feb 9, 2012 at 1:20
  • Then use a Repeater displaying a list of question objects. (Repeater also creates an unknown number of controls programatically, but it also takes care of restoring them for you during a postback.) Commented Feb 9, 2012 at 1:24

1 Answer 1

3

You don't specifically say what type of .net development you are doing, and your question is tagged with asp-classic, which I doubt you're using. [If you are please please stop] So I will assume you are using Web Forms.

While I don't agree with Inerdial's position that you should avoid dynamic controls at all costs, I will say it does make things much more complex and requires a very good knowledge of the ASP.net Lifecycle. If you truly want to go down that path, here is a great resource.

With that said what you are describing to me does not appear to need that and his suggestion of setting the control visibility to false is a good one.

You could create a row, a panel or a div and output the controls that you need when adding a new row and set it's server-side visibility to false whenever you don't want it displayed. Then you could have a link that when clicked it toggles the visibility to true and will allow the user to add items. Once users add items they'll be displayed in your data table and you can reuse the form to add additional items.

I would also like to encourage you to consider JavaScript if it isn't an overly complex form. It eliminates an extra round-trip to your server and in general is a better user experience.

Edit: This link may also be of use to you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.