HighTechTalks DotNet Forums  

Problem with EventHandlers

Dotnet Framework microsoft.public.dotnet.framework


Discuss Problem with EventHandlers in the Dotnet Framework forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
AT
 
Posts: n/a

Default Problem with EventHandlers - 06-21-2006 , 03:22 PM






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.


Reply With Quote
  #2  
Old   
Cowboy \(Gregory A. Beamer\)
 
Posts: n/a

Default Re: Problem with EventHandlers - 06-21-2006 , 08:19 PM






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!
*************************************************
<matt.grande (AT) gmail (DOT) com> wrote

Quote:
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.




Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.