O'Reilly logo

Microsoft® Visual Basic® .NET Programmer's Cookbook by Matthew MacDonald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Add Controls to a Web Form Dynamically

Problem

You want to add a Web control to a Web page at run time and handle its events.

Solution

Create a control object, add it to the Controls collection of a container control, and use the AddHandler statement to connect any event handlers. You must create the control after every postback.

Discussion

You can use a similar technique to add Web controls to a Web page as you would use to add Windows controls to a form, but there are some differences, including:

  • Dynamically added controls will exist only until the next postback. If you need them, you must re-create them when the page is returned. This requirement does not prevent you from handling their events, however.

  • Dynamically added controls are not as easy to ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required