Confused about writing Page Events in C# ASP.NET

K

kellygreer1

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
(e-mail address removed)
change nospam to yahoo
 
C

Coskun SUNALI [MVP]

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);
}
}



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

Scott Roberts

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);
}
}

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.
 
K

kellygreer1

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);
}
}

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
 
S

Scott Roberts

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.
 
P

Phil H

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
(e-mail address removed)
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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top