6

Here is the issue:

I have a simple ASP.NET form with 2 buttons.

One of them is created by dragging the button from the tools and the other I created directly in HTML:

<body>
<form id="Form1" method="post" runat="server">
    <asp:Button OnClick="ABC" Runat="server" Text="rrr" id="Button1"></asp:Button>
    <asp:Button  id="Button2" runat="server" Text="Button"></asp:Button>
</form>
</body>

Button2 is created using tools and has the following Event Handler:

 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim s As String = ""
 End Sub

This "Private Event handler runs without any problem.

However for the button which is created under HTML , we have the following event handler:

Private Sub ABC(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim a As Integer = 0

End Sub

For this one I get the following complile error:

Compiler Error Message: BC30390: 'WebApplication9.WebForm1.Private Sub ABC(sender As Object, e As System.EventArgs)' is not accessible in this context because it is 'Private'.

If I change the event handler scope from Private to protected this will work. Question is why private works for one event handler but not the other one.

5
  • 2
    Having OnClick and Handles means you're trying to hook it up twice - once from the code behind, and once from the HTML. If you switch to Protected, you'll find that the event handler is invoked twice (as you currently have it). Remove the OnClick in the HTML Commented Jun 14, 2013 at 12:50
  • I just tried to add "Hanldes" after I saw the compile error. I get same compile error with or without the Handler keyword.
    – S Nash
    Commented Jun 14, 2013 at 12:55
  • I said remove the OnClick in the HTML, not the Handles. Commented Jun 14, 2013 at 12:55
  • This situation is very simple to replicate. Anyone can setup and replicate the above scenario in 2 minutes.
    – S Nash
    Commented Jun 14, 2013 at 12:56
  • If I remove the OnClick then, it is like I have dragged 2 button to my form from the toolbox(codewise). I wanted to know why one of these works with private and otherone needs Protected.
    – S Nash
    Commented Jun 14, 2013 at 13:02

1 Answer 1

10

Basically, an aspx page is implemented as two classes. One of these classes contains your code behind code (.aspx.vb) (and, depending on which version/model of ASP.Net you're using, also some designer generated code (.aspx.designer.vb)).

The second class is created when the page is first requested (or the site is pre-compiled) and contains any inline code from the .aspx page and other code generated by ASP.Net, and includes e.g. code for any controls declared with runat="server".

This second class inherits from the first.

So, if the first class takes responsibility for hooking up its event handlers, it uses a Handles clause*:

Private Sub ABC(...) Handles Button1.Click

Button1 belongs to this class because it was put there by the designer generated code. Everything is local to this class, and so the method can be Private.

If the second class takes responsibility for hooking up an event handler, it does it by using attributes on server controls, such as here:

<asp:Button OnClick="ABC" Runat="server"

Now, unless ABC is a method declared inline inside the .aspx file, it has to be from the first class (or any class from which the first itself inherits from)

We now have a situation where code in the second class wants to refer to code in the first class. And so, the rules of .NET say that the member that it's trying to access cannot be Private.


What you shouldn't have, as you have in your question, is both classes taking responsibility for hooking up the (same) event handlers.


*It doesn't have to use a Handles clause - it could also set up the event handler using AddHandler inside e.g. the Page_Load event, or anywhere else that's appropriate. Handles is idiomatic for static controls on a page in VB. In C#, there's no equivalent to Handles, so the event handlers are hooked up with C#'s equivalent of AddHandler, +=.

1
  • Good explanation but not everything declared is privated: Button1 itself is declared as Protected: Protected WithEvents Button1 As System.Web.UI.WebControls.Button
    – S Nash
    Commented Jun 14, 2013 at 13:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.