views:

366

answers:

2

I have a MasterPage, with my Usercontrol inside a div.

I can set visible=false to the UserControl and to the containing div, and this works fine. But the Page_Load of the UserControl is always hit.

Is this by design, or am I missing how to stop page execution going into the Page_Load method of the UserControl.

+1  A: 

When you add a UserControl to a page it will be instantiated and added to the Page.Controls collection every time the page is executed. When you set Visible to false it basically just short circuits the rendering of the control's html so that it doesn't show on the page.

If there is some expensive operation that your control does in "Page_Load" I would key off of the Visible property to circumvent that operation.

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

joshperry
A: 

You cant stop execution going into the PageLoad event method for a user control.

@joshperry suggestion to use Visible property to determine whether or not to do a time consuming operation in the code is a good one.

The other option would be to complete the time consuming option in the OnPreRender event method.

Paul Rowland