Dynamic user controls in asp.net

Hello, I am trying to understand how to create dynamic user controls in asp.net.

I just know that this type of control is created or loaded at runtime.

Does anyone know a good tutorial on this topic?

thanks in advance,

+4
source share
4 answers

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

  • Put a reasonable fixed number of controls on the page, and then show only the ones you need.
  • Find out the source for the dynamic controls and divert it to the data source (array, ienumerable, list, etc.) that you can bind to the relay, even if it's just a call to Enumerable.Range ().
  • Create a custom control that outputs the html that you want, bypassing all of the "control" metaphor for this content.

If you really need to work with dynamic controls, it is important to preserve the nature of the lack of stateless http, as well as the life cycle of an asp.net page . Each of them adds its own wrinkle to make dynamic controls work: the first thing you need to create or recreate the controls every time you perform a postback, and the last thing you need to do is before you get into the page load event - usually in init or pre-initialization mode.

+6
source

Usually people talk here about dynamically creating an instance and adding a control to the placeholder.

for instance

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

The above instance of MyControl.ascx places it in a placeholder with the identifier myPlaceholder.

+4
source

I agree with @Joel, knowing the life cycle of the page, the nature of statelessness, etc., you can avoid traps. The main thing to pay attention to, what I had to do is:

  • Page_Init - initialize the controls that are on the page here, since they were the last time you displayed the page. This is important because the ViewState starts after Init and requires the same controls to be initialized the same as the previous ones. You can load the control using the code from @Mitchel ie

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

  • Page_Load. Download the contents of the controls here, as with any control that does not load dynamically. If you saved a link to them in your page_init, they will be available here.

Saving this structure I had too many difficulties, because it is similar to how ASP.NET was designed to work, even if all the samples in MSDN do not do it this way. The most important thing you need to observe is to keep track of the state of the page in relation to the controls you have provided.

In my case, he took the section number of the multi-page survey and reloaded the questions from the database, so all I had to do was keep track of the current section number, which was not complicated.

Having said all this, if you use dynamic controls to show and hide different views of the same screen, I suggest you not to use them. In this case, I would rather use either user controls (with inappropriate hidden ones) or placeholders to mark areas that have not yet been displayed, or individual pages / views, etc. So that you save pages to one responsibility, which makes it easier to debug and / or get useful information from the user about which page they were on.

+1
source

The Microsoft article is very good, but the best article I read is in the following link:

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

If you are really interested in the dynamic elements of ASP.NET Web Forms, I recommend that you study the DotNetNuke CMS portal. DotNetNuke is one of the best cases of using dynamic controls as the main function for creating dynamic portals and pages using ASP.NET portals. It is free to download at www.dotnetnuke.com.

I hope this helps

0
source

Source: https://habr.com/ru/post/1336846/


All Articles