简体   繁体   中英

what is the best way not to load UserControls on Page Load

I have a general question. My page has several quiet 'heavy' UserControls, each of them is placed in a MultiView View. On PageLoad each of these controls initialize, causing unnecessary database calls. What I want is to load only that control whose View is set to 'Active'. Now I use public boolean properties in each of these user control to set whether to bind data or not, but is there a common method to do this?

This is how I do it (similar to your solution)

Each control implements a method for databinding. This method should only be called if the view it is displayed on is 'Active'. (Like you are already doing on the serverside). Each of these controls only contains a Literal into which I render my HTML from the codebehind.

I'd also like to have cleaner way, but controls have their OnLoad et cetera methods called even if they are invisible. From my knowledge you have to perform such a databinding operation or setting the value of a property manually.

You should not override the DataBind method, because this will be called even if the control itself has its Visible property set to false. (That's how I remember, but you can check just to make sure). Are more clean way would probably be to check for your property in the overriden DataBind method and only perform your databinding if it is set to true.

If you truly don't want the UserControls involved in the ASP.NET Page Life Cycle on each load/postback then dynamically load them when needed.

Dynamic Loading of ASP.NET User Controls

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM