Problem with EventHandlers

  • Thread starter Thread starter matt.grande
  • Start date Start date
M

matt.grande

Please, I REALLY need help with this one. I've been banging my head
about it for three days now.

Here's the situation.

I have a form that runs some queries and functions and such. When all
is said and done, there are several new controls (LinkButtons) on the
page. Dynamicly created controls MUST be created in the Page_Load.

Now, if I click one of the LinkButtons, the page attempts to reload
before calling the button's EventHandler. This causes the
above-mentioned function to go again, before the EventHandler is
called. This causes an unnecessary slow-down.

I need to find a way to skip over the method. The best solution I've
been able to come up with is as follows:

Code:
string et = Request.Form["__EVENTTARGET"];
if ((IsPostBack) && (et == ""))
ExecuteCustomSearch();

One would think that it would work fine, but for whatever reason, if
the "if" statement is false, the EventHandler doesn't get called.

I'm at my wits end here. Any help would be greatly appreciated.
 
I would have to see more to give you a better idea of what you are trying to
do, but I am betting there is a better architecture. Here is off my head:

Page_Load()
{
if(!PostBack)
CreateDynamicControls();
}

In the control in your question, create a delegate. Call this delegate from
the event handler. Handle the delegate in your page code, by creating a
delegate (or event) handler. In this handler, recall the
CreateDynamicControls() method. If the controls have to be on the page prior
to your control, add container controls to hold them, like a Panel (or
Panels). You can attach controls to a container dynamically at any point
prior to flushing the page to the client.

Hope this makes sense.

Overall, you should never have a huge amount of code in Page_Load, esp if
there are complex decision trees. It is better to refactor the code out so
you can call it from multiple locations. Here is the basic Page_Load()
pattern:

Page_Load
{
if(!IsPostBack)
//code that runs when there is a GET (initial page load)
else
//code that is run every time the page is postback
//or Conditional code that deals with loading the page and
//not event handling or something that can be handle
//by an event
}

If you are placing complex trees in Page_Load(), you are most likely doing
it poorly. I hope this does not sound harsh, but the purpose of Page_Load()
is to load the page, not handle everything you do to paint the page.

Hope this helps.


--
Gregory A. Beamer

*************************************************
Think Outside the Box!
*************************************************
 
Back
Top