简体   繁体   中英

Dynamically add textbox in asp.net

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM