1

In My VB.NET web page, I have this standard event. Note the "Handles" clause on teh event declaration.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub

In my C# web app, I have this:

protected void Page_Load(object sender, System.EventArgs e)
{

Since C# doesn't have a "Handles" equivalent and from what I've seen, event handlers are wired up using delegate += syntax, I was looking for this, but I could not foind it in the aspx page, aspx.cs file or the aspx.designer.cs file.

In VB, I would have two drop down lists at the top of the Code Editor window and I could select any object on the web form or the web form itself and see the possible events for the object. Selecting the event would either take me to the event handler or if it didn't exists, it would create the stub for me.

I know that the Properties window in C# (and I think in VB, too) has an Event tab that shows the list of events for the selected object GUI object, but "Page" doesn't appear as an object that can be selected.

  1. Where does C# define the hooking up of the event to the handler?

  2. How do I generate a stub for the Page event handler routine? I know that the handle appears by default, but what if it is deleted or I want to add a Page_initialize code? Is there an easy way to get the stub or do I need to go to the Object Browser for the syntax?

2
  • 1
    1. PagesSection.AutoEventWireup 2. manually Commented Jul 8, 2012 at 19:26
  • Re: 1: Interesting. I like explicitness. Re: 2, I understand this approach for controls on a form, but how would you take this approach for a form? For example, is there no easy way to introduce a pre-render event handler to my form? Your comment is the best answer so far. Commented Jul 8, 2012 at 19:36

3 Answers 3

1

In C# web forms, the @Page directive AutoEventWireup property on the markup code behind is defaulted to true, as opposed to false for VB. To see the @Page directive and all of its associated properties, right click on your web page in Solution Explorer and choose 'View Markup'

With AutoEventWireup=true, the runtime will automatically connect the event handlers it finds in your code that match the naming convention form of Page_EventName. You can however turn off this functionality and wire up the page event handlers manually using the standard C# += assignment. If you are using the AutoEventWireup=true, not only must your method name match, but obviously it must also have an appropriate method signature in order to be wired up automatically by the runtime.

See this KB for a good discussion of AutoEventWireup: http://support.microsoft.com/kb/324151

With respect to your second question, in C# there is no way to generate "stubs" for page events like there is in VB. As other have noted, including yourself -- there is similar functionality in C# for generating control object event stubs, via the property window. However, for page events you must know the event name and appropriate signature and code it yourself.

Sign up to request clarification or add additional context in comments.

1 Comment

Very articulate and complete answer, thank you! I'm a little disappointed in this lack of tooling functionality on the C# side.
1

Where does C# define the hooking up of the event to the handler?

Page_Load is a special event that is automatically hooked up. It's a reserved name. So there's nothing you need to do for this event to be hooked up. Just declare it in the code behind.

Comments

-1
namespace MyNamespace
{
    public class Myclass : System.Web.UI.Page
    {
        override protected void OnInit(EventArgs e)
        {
            this.Load += new System.EventHandler(this.Page_Load);
        }

        private void Page_Load(object sender, EventArgs e)
        {
        }
    }
}

Reference: https://support.microsoft.com/en-us/help/324151/how-to-use-the-autoeventwireup-attribute-in-an-asp-net-web-form-by-usi

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.