Dynamic User Controls in asp.net

后端 未结 4 458
死守一世寂寞
死守一世寂寞 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:36

    The Microsoft article is very good, but the best article that I have been read is in the bellow link:

    http://www.4guysfromrolla.com/articles/092904-1.aspx

    If you are really interested in ASP.NET Web Forms dynamic controls, I recommend that you study the DotNetNuke CMS Portal. DotNetNuke is one of the best cases using dynamic controls as your core feature to build dynamic portals and pages using ASP.NET Portals. It is free for download in www.dotnetnuke.com.

    I hope it helps

    0 讨论(0)
  • 2021-01-13 20:44

    Typically what people are talking about here is the dynamic instantiation and addition of a control to a placeholder.

    For example

    Control ControlInstance = LoadControl("MyControl.ascx");
    myPlaceholder.Controls.Add(ControlInstance);
    

    The above instantiates MyControl.ascx and places it inside of a placeholder with id of myPlaceholder.

    0 讨论(0)
  • 2021-01-13 20:45

    I agree with @Joel by knowing the page lifecycle, the stateless nature in mind etc it is possible to avoid the pitfalls. The main things to watch out for, which I have had to do, are:

    1. Page_Init – initialise the controls that are on the page here as they were the last time you rendered the page. This is important as ViewState runs after Init and requires the same controls initalised the same way as the way they were previously rendered. You can load the control using the code from @Mitchel i.e.

      Control ControlInstance = LoadControl("MyControl.ascx"); myPlaceholder.Controls.Add(ControlInstance);

    2. Page_Load – Load the content of the controls in here as you would with any control that isn’t dynamically loaded. If you have kept a reference to them in your page_init they will therefore be available here.

    Keeping to this structure I haven’t had too much difficulty as this is appears to be the way that ASP.NET was designed to work, even if all the samples on MSDN don’t do it this way. The biggest thing that you then have to watch is tracking what state the page was in in regard to the controls that you have had rendered.

    In my case it was take the section number of the multipage survey and reload the questions from the database, so all I had to do was track the currently rendered section number which wasn’t difficult.

    Having said all that if you are using dynamic controls just to show and hide different views of the same screen then I suggest you don’t use them. In this case I would much rather use either user controls (with the inappropriate ones hidden), placeholders to mark areas that aren’t to be rendered yet, or separate pages/views etc. as that way you keep the pages to a single responsibility which makes it easier to debug and/or get useful information from the user about which page they were on.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题