views:

85

answers:

2

Hey guys,

I have a question about wiring web server controls. From many of the examples that I have seen the event handler has been declared with a private access modifier. I tried doing so as shown:

    <asp:Label runat="server" ID="lblMessage" Font-Names="Verdana" Text="Hello" />
        <br />
    <asp:Button runat="server" ID="btnSubmit" text="click me!" onClick="btnSubmit_Click" />

and in the code behind file:

    private void btnSubmit_Click(object sender, EventArgs e)
    {
        lblMessage.Text = "Goodbye";
    }

But the compiler is unable to locate the click handler unless i change the access to protected.

Should button event handlers ever be private, and if so, why did it not work in my instance?

Also, other than using the onClick property, are there any other methods for wiring event handlers to controls that are declaratively created in the .aspx file?

Thanks in advance, Yong

+4  A: 

What might be confusing here is that many people design their web forms visually. When they want to wire a click handler to a button server control, they typically either double click the button or select it in Visual Studio, press F4 to go to its properties and set its events within that window. Either of these approaches has the following effect:

  1. A private handler is created in the code behind class. Its name is [control name]_[event]. In your case, this is btnSubmit_Click.

  2. Also in the code-behind file, each web server control has a corresponding protected member of the code behind class. In the Init even, the newly created handler is associated to the Click event on the server control by the following code:

    btnSubmit.Click += btnSubmit_Click;

With this approach, the handler can still be private. Where you are having an issue is when you try to set the handler in the ASPX file itself. In this case, you will have to update the event handler to be non-private, as you've indicated, or define the following in the ASPX page itself:

<script language="C#" runat="server">
  public void btnSubmit_Click(object sender, EventArgs e)
  {
    ..
  }
</script>

Private methods/properties of a code-behind class are not visible to the ASPX page.

David Andres
Thanks David, I guess that sort of underlines one drawback to visual studio's ability to automate so much of the coding process, it masks what is in fact happening underneath and a lot of the time and you lose that deeper understanding. Great explanation.
yongjieli
The private handler behavior only applies to Visual Studio 2003, 2005+ writes it into the ASPX file and creates a protected method AFAIK.
Wyatt Barnett
@Wyatt: Good to know.
David Andres
+1  A: 

The event handler needs to be protected. The reason is that ASP.NET actually generates a new class that inherits from the class you define in your codebehind, rather than using the class itself.

As an alternative, you could write:

btnSubmit.Click += new EventHandler(btnSubmit_Click);

In the Page_Load handler, but I wouldn't recommend it. In VB.NET, I believe you could use the Handles keyword as well, but it's not available in C#.

Thorarin