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 / ASP.NET / General / February 2008

Tip: Looking for answers? Try searching our database.

Confused about writing Page Events in C# ASP.NET

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
kellygreer1 - 06 Feb 2008 16:59 GMT
So by default the editor only throws in "protected void Page_Load"
event.
What is the correct way to add in other events.

I had in the past copied the events from other pages where I had
manually typed them in such as:

   protected void Page_LoadComplete(object sender, EventArgs e)
   {

   }

So recently I started to skip this ... thinking there was an easier
way... and I started wiring these up in the Page_Load event. Example:

  protected void Page_Load(object sender, EventArgs e)
   {
       this.Page.LoadComplete += new EventHandler(Page_LoadComplete);
   }

This is great because it throws in all the scaffold-like code that I
need.
But I just noticed today that this causes my Page.LoadComplete method
to be run twice!

What is the proper way to do this stuff in C# ASP.NET 2.0?  I know
VB.NET has the nice dropdown at the top that helps you declare the
Page events. What is the right way to do this in C#?

Thanks,
Kelly Greer
kellygreer1@nospam.com
change nospam to yahoo
Coskun SUNALI [MVP] - 06 Feb 2008 17:24 GMT
Hi,

You can write "override" and when you press space, intellisence will start
working. Then you can write which event you want to override. For example:

   protected override void OnLoadComplete(EventArgs e)
   {
       base.OnLoadComplete(e);
   }

And depending on your choice, you can write your code before, after or
before and after executing the "base.OnLoadComplete" method. Also, you can
completely remove it if you don't want it to execute anything in the base
class.

Other than that, you may want to add a IsPostBack control before hooking the
event in Page Load:

   protected void Page_Load(object sender, EventArgs e)
   {
       if (!Page.IsPostBack)
       {
           Page.LoadComplete += new EventHandler(Page_LoadComplete);
       }
   }

Signature

All the best,
Coskun SUNALI
MVP ASP/ASP.NET
http://sunali.com
http://www.propeople.dk

> So by default the editor only throws in "protected void Page_Load"
> event.
[quoted text clipped - 29 lines]
> kellygreer1@nospam.com
> change nospam to yahoo
Scott Roberts - 06 Feb 2008 17:42 GMT
> Other than that, you may want to add a IsPostBack control before hooking
> the event in Page Load:
[quoted text clipped - 6 lines]
>        }
>    }

It seems like the "Page_LoadComplete executing twice" problem would stem
from having AutoEventWireup set to true and then wiring up the event in page
load too, not from IsPostBack.
kellygreer1 - 06 Feb 2008 17:50 GMT
> Hi,
>
[quoted text clipped - 60 lines]
> > kellygre...@nospam.com
> > change nospam to yahoo

Sorry two follow up questions:
Why does "Page_Load" or  "Page_LoadComplete" even work without the
wiring (explicitly setting the EventHandler)?

How many of these things are "AutoMagically" triggered by the ASP.NET
engine?
This is the only way I can figure that one method was fired twice.

Kelly
Scott Roberts - 06 Feb 2008 18:46 GMT
> Sorry two follow up questions:
> Why does "Page_Load" or  "Page_LoadComplete" even work without the
> wiring (explicitly setting the EventHandler)?

Search help for AutoEventWireup. You will probably find
"AutoEventWireup=true" in your @Page declaration in the .aspx page. You can
set it to "false" to disable the behavior.

> How many of these things are "AutoMagically" triggered by the ASP.NET
> engine?

I believe that it is only Page events.
Phil H - 06 Feb 2008 23:54 GMT
> So by default the editor only throws in "protected void Page_Load"
> event.
[quoted text clipped - 29 lines]
> kellygre...@nospam.com
> change nospam to yahoo

For C# the easiest way to create event handlers other than the most
common ones (e.g. click event for say a LinkButton where all you have
to do is double-click the object in Design view) is to open up the
Properties window and select the control. At the top of the properties
window there is a control button with a sort of yellow jagged shape
icon. Click that and you will see a list of events rather than the
usual properties. Double-click the event that you wish to code and the
code page will be opened with the outline for the event already
created, (just like in VB when you use the drop-down lists at the top
of the code page).

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.