Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / June 2006

Tip: Looking for answers? Try searching our database.

Problem with EventHandlers

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
matt.grande@gmail.com - 21 Jun 2006 20:22 GMT
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();
[/code]

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.
Cowboy (Gregory A. Beamer) - 22 Jun 2006 01:19 GMT
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.

Signature

Gregory A. Beamer

*************************************************
Think Outside the Box!
*************************************************

> Please, I REALLY need help with this one.  I've been banging my head
> about it for three days now.
[quoted text clipped - 23 lines]
>
> I'm at my wits end here.  Any help would be greatly appreciated.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.