How do I stop the execution that is part of the UserControls PageLoad method?

I have a MasterPage with my Usercontrol inside a div.

I can set visible = false for the UserControl and for the containing div and that works great. But Page_Load UserControl always gets hit.

Is this by design, or am I missing how to stop the page execution in the Page_Load method for the UserControl.

+1


source to share


2 answers


You cannot stop the execution that is included in the PageLoad event method for a custom control.

@joshperry suggests using the Visible property to determine whether to perform multiple operations in your code.



Another option is to complete the time setting in the OnPreRender event method.

+1


source


When you add a UserControl to a page, it will be created and added to the Page.Controls collection every time the page is executed. When you set Visible to false, it basically just closes the passing of the html control so that it doesn't appear on the page.

If there is some expensive operation your control is doing on Page_Load, I would disable the Visible property to bypass this operation.



If the control was never created, how could you tell it is invisible?

+1


source







All Articles