Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why a private event handler does not work in ASP.NET

Tags:

asp.net

vb.net

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.

like image 883
S Nash Avatar asked Jun 14 '13 12:06

S Nash


People also ask

What can trigger execution of the event handler?

Event handler code can be made to run when an event is triggered by assigning it to the target element's corresponding onevent property, or by registering the handler as a listener for the element using the addEventListener() method.

How do you call an event handler in C#?

In C# 6.0 and above you can use Null Propagation: handler?. Invoke(this, e); handler(this, e) will call every registered event listener.

What is an event handler How does an event handler work in a typical application?

In programming, an event handler is a callback routine that operates asynchronously once an event takes place. It dictates the action that follows the event. The programmer writes a code for this action to take place. An event is an action that takes place when a user interacts with a program.


1 Answers

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, +=.

like image 167
Damien_The_Unbeliever Avatar answered Oct 26 '22 23:10

Damien_The_Unbeliever