question

RodAtWork avatar image
0 Votes"
RodAtWork asked cooldadtx commented

Getting error BC30506 on an old ASP.NET WebForms project

I maintain an old ASP.NET WebForms app written years ago by someone else. It appears that it was written using .NET 2. (I had to piece it together, because it wasn't a complete Visual Studio solution.) I updated it to .NET 4.5.2, which is the .NET Framework used here.

Now I'm getting this error on lots of pages:

BC30506 Handles clause requires a WithEvents variable defined in the containing type or one of its base types.

In researching this error I came across this page: https://forums.asp.net/t/1147454.aspx?BC30506+Handles+clause+requires+a+WithEvents+variable+defined+in+the+containing+type+or+one+of+its+base+types+

However, the solution given has already been covered by the original developer. He defined the CodeFile and it inherits from AccessLogs. Here's the top of one page raising this error:

 <%@ Page Language="VB" AutoEventWireup="false" CodeFile="AccessLogs.aspx.vb" Inherits="AccessLogs" %>

And here's the beginning of the code behind:

 Imports System.Configuration.ConfigurationManager
    
 Partial Public Class AccessLogs
     Inherits System.Web.UI.Page

What else could be the solution to this problem?






dotnet-aspnet-webforms
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @RodAtWork ,
According to your codes,the btnShow variable must be defined with the WithEvents keyword.Just like this:

 Private WithEvents btnShow As New Button()

More details,you could refer to below article:
https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/error-messages/handles-clause-requires-a-withevents-variable-defined

Best regards,
Yijing Sun

0 Votes 0 ·
cooldadtx avatar image
0 Votes"
cooldadtx answered cooldadtx commented

Based upon the docs this is caused by your code behind file not including the WithEvents keyword on the field declaration for the event you're trying to handle.

Private Sub HandleAnEvent ( sender As Object, e as EventArgs ) Handles _field.SomeEvent
End Sub

' Compiler error
'Private _field As New SomeControl()

' Correct
Private WithEvents _field As New SomeControl()


Please post the relevant code it is having an issue with including the field declaration and where the event is being handled.

· 6
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Here's an example from the AccessLogs page:

 Protected Sub btnShow_Click(sender As Object, e As System.EventArgs) Handles btnShow.Click
    
     Dim AccessLog As New PJAWic.AccessLog(AppSettings.Item("AccessLog"))
     AccessLog.Read()
     dgAccess.DataSource = EntryView(AccessLog)
     dgAccess.DataBind()
    
 End Sub

The error is being raised on the first line (in this case), the Sub declaration line. The other lines in the Sub don't raise error BC30506.

0 Votes 0 ·

Go to the declaration for btnShow. It must be declared with the WithEvents keyword otherwise you get the compiler error.

Private WithEvents btnShow As New Button()


You'll need to do this for all your forms/classes that use the Handles keyword. You are better off grepping your code and fixing all the cases rather than relying on the compiler errors.

0 Votes 0 ·

There is no other declaration for the btnShow button in the code behind, other than what I've already shown. Specially there's no

 Private btnShow As New Button()

anywhere in the code behind, for me to change it to

 Private WithEvents btnShow As New Button()


0 Votes 0 ·
Show more comments
AgaveJoe avatar image
0 Votes"
AgaveJoe answered cooldadtx commented

In VB, the Handles keyword works in conjunction with the class members defined using the WithEvents keyword. In newer ASP.NET application this "control wire up" is generated automatically in the filename.aspx.designer.vb file (Partial class) when adding a control to an editor window; design or source. If I recall, in ASP.NET 1.1 the source code was in the code behind file. I'm pretty sure 2.0 an onward uses the designer file and partial classes. Anyway, I think you are missing the designer file(s) which contains the page class members like the TextBox, GridView, etc.

Create a new VB web application project. Drag a control to a web page then take a look at the designer file; filename.aspx.designer.vb.

· 18
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

There are no filename.aspx.designer.vb files in any of the projects, in the solution.

There are some filename.designer.vb, though.

This makes me wonder, could the original application have been a Web Site, instead of a Web Application? Like I've said I had to put this application together, because the original developer didn't save it in any version control system, nor did he keep it together anyway. I had to piecemeal it together. I don't know if the original application was a Web Site or a Web Application.

0 Votes 0 ·

Hi @RodAtWork ,
If you loss your .design.cs file,you could regenerate the file. Right Click on you aspx or ascx file and select "Convert to Web Application". As soon as you do this a new .designer.cs file will be created for you with all the controls in it.

I don't know if the original application was a Web Site or a Web Application.

Web application is a piece of software that can be accessed by the browser. A Browser is an application that is used to browse the internet.
Web application needs authentication. The web application uses a combination of server-side scripts and client-side scripts to present information. It requires a server to manage requests from the users.
Example: Google Apps

Website: Website is a collection of related web pages that contains images, text, audio, video, etc. It can be consist of one page, two pages, and n number of pages.
A website provides visual and text content that users can view and read.
To view a website requires a browser(chrome, firefox). There are many types of websites like Archive website, Blog, Community website, Dating website, etc.
Example: Amazon, youtube, etc.

Best regards,
Yijing Sun

0 Votes 0 ·

Hi @YijingSun-MSFT,

So, if there's no .designer.vb files does that imply the app was originally created as a ASP.NET Web Site? (Not an ASP.NET Web Application.)

0 Votes 0 ·
Show more comments