Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I Stop Page_load for a hidden user control?

Status
Not open for further replies.

TWillard

Programmer
Apr 26, 2001
263
US
I have read posts about the design of asp.net web pages. From what I hear, one of the "current" or "best" designs is to use a single page and use pannels.

I am attempting to use this technique for my page. A user can enter into my page and see one of three different pannels. One of my pannels contains a user control. This user control's page_load event gets fired even if the pannel visible property is set to false. There is background data processing logic that gets fired within the page_load event of the user control. I do not wish for this processing to occur except when that particular pannel is displayed.

Any idea on how to prevent this?

 
I have found one solution that works, (see code below). This code calls for the removal of the user control. However, this code is required in the two pannels that do not contain the user control. This seems a bit of hacky to me. The two other pannels should not have to concern themselves with the existing objects in other panels.

Me.Pannel1.Controls.Remove(Me.Pannel1.FindControl("UserControl1"))


Any other ideas?
 
Another solution is to scratch the user control. Move the code that existed in the user control into the panel. I feel that this too is hacky. This defeats the purpose of reusability in a user control.

I welcome other ideas. I would love to hear other ways to solve this problem.
 
Being that Page_Load is an event that is going to get raised regardless of what you do, then I don't know of a way to short-circuit the running of the EventHandler. The event is going to get raised, and the handler will be fired (AFAIK).

That said, the other way to handle this would be to move the logic into another method besides Page_Load, and then call that method manually from the parent page. Often times, I'll put the meat of a uc in a LoadIt() method, and then just call it from the parent page:

MyUC.LoadIt();

In this way, you control when the code gets executed, too, which can have its own benefits, even if you want it executed on every load.

-p

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
I don't think it really hurts anything for the event to fire as long as the code in it only runs when you want it to. Can you somehow check that the desired panel is visible by wrapping the page load code in an if then statement? The event will still fire, but won't do anything. It is still a good idea to move the code to another method like link9 said and just call it from the page load. I wish I had done that when I started on my current project. It makes for much more readable (and reusable) code.
 
I agree with jen.

if( this.Visible )
{
//do stuff
}
else
{
//do nothing
}
 
Thanks for all of the responses. That was exactly what I was looking for. link9 is right once again!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top