469,568 Members | 1,483 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,568 developers. It's quick & easy.

Confused about writing Page Events in C# ASP.NET

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
ke*********@nospam.com
change nospam to yahoo
Feb 6 '08 #1
2 5041
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.

Feb 6 '08 #2
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.

Feb 6 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Daniel | last post: by
3 posts views Thread by C CORDON | last post: by
3 posts views Thread by Michael Tissington | last post: by
7 posts views Thread by AndrewMBaldwin | last post: by
5 posts views Thread by Carlo Marchesoni | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.