Getting error BC30506 on an old ASP.NET WebForms project

Rod At Work 866 Reputation points
2021-09-22T18:05:19.567+00:00

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?

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,271 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Taylor 48,581 Reputation points
    2021-09-22T18:40:10.157+00:00

    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.


  2. AgaveJoe 26,136 Reputation points
    2021-09-22T18:44:38.953+00:00

    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.