简体   繁体   中英

ASP.NET:Adding a collection of controls dynamically

here is the requirement:

i have a lable a textbox, and two buttons(+ and - button) on my asp.net page.

so when i click on the plus button it should add the the whole collection of controls to the page(ie lable, textbox and two buttons). And when i click on minus button it should remove the collection.

This should go on.meaning whenever i click on any plus button the control collection should get added to the page. I am wondering what will be the best approach to do it.

Any suggestion?

You could use postbacks and add your controls server-side, you could even use ajax to make it more seamless.

If you are going to go this way, remember to re-create your dynamic controls on each postback

If post-backs don't bother you, then just do what you mentioned on server side in response to button click event.
Have some container, add and remove components dynamically, attach your event handlers to events of newly created objects.

Wrap the controls you want to show/hide in a panel. Then in the click event handlers of the buttons set the Visible property of the panel to true or false as you need.

Beware that your + button will need to be outside the panel otherwise you'll never get them back again.

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