Revisiting AutoEventWireup

G

gnewsgroup

I know that enough has been written/talked about AutoEventWireup.
I've read the MSDN documentation about AutoEventWireup, which turns
out to be very brief.

I've also googled and read quite many articles/conversations about
this topic. I am still confused. Here are my questions:

1. Is it the case that only Page_* (e.g. Page_Load, Page_Init,
Page_PreRender, etc.) events are relevant with regard to
AutoEventWireup? In other words, this has nothing to do with, for
example, the Click event of a Button?

2. I created a very simple application, whose code-behind looks like
this:

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("hola, and how is it going?");
}

// The ID of the Button is btnSubmit in the aspx page.
protected void btnSubmit_Click(object sender, EventArgs e)
{
lblMessage.Text = TextBox1.Text;
}
}

If I set AutoEventWireup=false, Page_Load is not executed and I don't
see "hola, and how is it going?" in the browser. This message shows
up when AutoEventWireup=true.

This is understood.

But, then * with AutoEventWireup=false *, how do I manually wire up
Page_Load to the Load event? I tried adding the following right
before the Page_Load method:

private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}

The "hola, and how is it going?" message still does not show up in the
browser. I must not be doing the right thing, please kindly advise.
Thanks.

I have more questions to come regarding AutoEventWireup.
 
G

gnewsgroup

I know that enough has been written/talked about AutoEventWireup.
I've read the MSDN documentation about AutoEventWireup, which turns
out to be very brief.

I've also googled and read quite many articles/conversations about
this topic. I am still confused.  Here are my questions:

1. Is it the case that only Page_* (e.g. Page_Load, Page_Init,
Page_PreRender, etc.) events are relevant with regard to
AutoEventWireup?  In other words, this has nothing to do with, for
example, the Click event of a Button?

2. I created a very simple application, whose code-behind looks like
this:

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("hola, and how is it going?");
    }

    // The ID of the Button is btnSubmit in the aspx page.
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        lblMessage.Text = TextBox1.Text;
    }

}

If I set AutoEventWireup=false, Page_Load is not executed and I don't
see "hola, and how is it going?" in the browser.  This message shows
up when AutoEventWireup=true.

This is understood.

But, then * with AutoEventWireup=false *, how do I manually wire up
Page_Load to the Load event?  I tried adding the following right
before the Page_Load method:

private void InitializeComponent()
    {
        this.Load += new System.EventHandler(this.Page_Load);
    }

The "hola, and how is it going?" message still does not show up in the
browser.  I must not be doing the right thing, please kindly advise.
Thanks.

I have more questions to come regarding AutoEventWireup.

Further reading online told me that I need to override the OnInit
method and call the InitializeComponent method in it. Like this:

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

I learned that setting AutoEventWireup to true will cause a
performance hit, and it is suggested that we set AutoEventWireup to
false when we deploy our web application.

But, does this imply that we then will have to provide the overrided
OnInit method in each page whose Page_Load does have some stuff in
it? It sounds very cumbersome to me.
 
D

Dina

I know that enough has been written/talked about AutoEventWireup.
I've read the MSDN documentation about AutoEventWireup, which turns
out to be very brief.

I've also googled and read quite many articles/conversations about
this topic. I am still confused. Here are my questions:

1. Is it the case that only Page_* (e.g. Page_Load, Page_Init,
Page_PreRender, etc.) events are relevant with regard to
AutoEventWireup? In other words, this has nothing to do with, for
example, the Click event of a Button?

2. I created a very simple application, whose code-behind looks like
this:

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("hola, and how is it going?");
}

// The ID of the Button is btnSubmit in the aspx page.
protected void btnSubmit_Click(object sender, EventArgs e)
{
lblMessage.Text = TextBox1.Text;
}

}

If I set AutoEventWireup=false, Page_Load is not executed and I don't
see "hola, and how is it going?" in the browser. This message shows
up when AutoEventWireup=true.

This is understood.

But, then * with AutoEventWireup=false *, how do I manually wire up
Page_Load to the Load event? I tried adding the following right
before the Page_Load method:

private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}

The "hola, and how is it going?" message still does not show up in the
browser. I must not be doing the right thing, please kindly advise.
Thanks.

I have more questions to come regarding AutoEventWireup.

try giving the form name in the event handler declaration like so...

this.Form1.Load += new System.EventHandler(this.Page_Load);

I think this should solve the problem...
 
G

gnewsgroup

try giving the form name in the event handler declaration like so...

this.Form1.Load += new System.EventHandler(this.Page_Load);

I think this should solve the problem...

Thanks, but you did not read my 2nd post.
 
D

David Anton

If you specify AutoEventWireup = true, then only the events starting with
"Page_" are automatically wired to methods of the same name.
However, this is only one of many options for wiring events:
1. AutoEventWireup = true - affects only "Page_*" events.
2. AutoEventWireup = false and specify "Page_*" event wireups manually.
3. Override the "On..." methods of the base class.
4. Specify event wireups in the control's markup section on the page.

Note that with AutoEventWireup = true, you just need either Page_Init or you
override OnInit, not both. With AutoEventWireup = false, you should override
OnInit.
--
http://www.tangiblesoftwaresolutions.com
C++ to C#
C++ to VB
C++ to Java
Instant C#: VB to C#
Instant VB: C# to VB
Instant C++ VB Edition: VB to C++/CLI
Instant C++ C# Edition: C# to C++/CLI
 
G

gnewsgroup

If you specify AutoEventWireup = true, then only the events starting with
"Page_" are automatically wired to methods of the same name.
However, this is only one of many options for wiring events:
1. AutoEventWireup = true - affects only "Page_*" events.

Thank you. So my guess was correct regarding this point.
2. AutoEventWireup = false and specify "Page_*" event wireups manually.
3. Override the "On..." methods of the base class.
4. Specify event wireups in the control's markup section on the page.

Note that with AutoEventWireup = true, you just need either Page_Init or you
override OnInit, not both. With AutoEventWireup = false, you should override
OnInit.

So, in other words, if we turn off AutoEventWireup for deployment, we
*do* have to add the overrided method OnInit to wireup the Page_Load
method, right? That's a lot of hassle.
 
D

David Anton

You can either override the OnInit (and wireup Page_Load there), or you can
just override OnLoad - some people use this approach in C#. In VB, they get
around this via the 'Handles' clause on the method, which takes care the
wireup behind the scenes.
--
http://www.tangiblesoftwaresolutions.com
C++ to C#
C++ to VB
C++ to Java
Instant C#: VB to C#
Instant VB: C# to VB
Instant C++ VB Edition: VB to C++/CLI
Instant C++ C# Edition: C# to C++/CLI
 

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

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top