Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic User Controls in asp.net

Tags:

asp.net

Hello I am trying to lear how to create Dynamic User Controls in asp.net.

I just know that this type of controls are created or loaded at run time.

Someone knows a good tutorial about this topic?

thanks in advance,

like image 641
Zorela Avatar asked Dec 22 '22 18:12

Zorela


1 Answers

The best thing you can learn about dynamic controls in ASP.Net webforms is how to avoid them. Dynamic controls in asp.net are filled with pitfalls. I almost always recommend one of the following alternatives:

  • Place a reasonable fixed number of controls on the page, and then only show the ones you need.
  • Figure out the source for the dynamic controls and abstract it out to a datasource (array, ienumerable, list, etc) that you can bind to a repeater, even if it's just a call to Enumerable.Range().
  • Build a user control that outputs the html you want, bypassing the entire "controls" metaphor for this content.

If you really must work with dynamic controls, it's important to keep the stateless nature of http in mind, along with the asp.net page life cycle. Each adds it's own wrinkle to making dynamic controls work: the former that you need to create or recreate the controls every time you do a postback, and the latter that you need to do this before hitting the page load event - usually in page init or pre-init.

like image 148
Joel Coehoorn Avatar answered Jan 09 '23 13:01

Joel Coehoorn