Dynamic User Controls in asp.net

后端 未结 4 457
死守一世寂寞
死守一世寂寞 2021-01-13 20:20

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 k

4条回答
  •  醉梦人生
    2021-01-13 20:49

    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.

提交回复
热议问题