in vb.net an error is coming - asp.net

Compiler Error Message: ASPNET: Make sure that the class defined in this code file matches the 'inherits' attribute, and that it extends the correct base class (e.g. Page or UserControl).
in vb code-i used namespace and a class.

The Inherits attribute in the Page directive of the aspx page must point to a class that implements the code-behind logic. If you have used extra namespaces, you will need to supply the fully qualified type name, so that it matches:
Inherits="The.Full.Namespace.Path.TheClassName"

Related

Access function member of base page class?

Currently I'm utilizing a base page class for my website declared like so:
Public Class BasePage
Inherits System.Web.UI.Page
and subsequent pages inherit that using:
Partial Class Default
Inherits BasePage
I have a function in the base page class that I want to be able to use from the pages that inherit the class. The function I have is declared:
Public Function GetSiteVers(ByVal parType As String) As String
When I attempt to call from a page that is inheriting the class:
Me.GetSiteVers("Misc")
I get the error
"GetSiteVers is not a member of Default"
or just
GetSiteVers("Misc")
I get the error:
'GetSiteVers' is not declared. It may be inaccessible due to its protection level.
I've different declarations and Vis Studio's intellisense finds no errors and when I build the page locally I get no compiler errors. Anyone have any ideas or suggestions?
The steps taken to correct this were:
Delete existing Basepage class file from App_code (BasePage.vb)
Reload page that was throwing the error (resuting in an obvious error, but forcing a recompile of the web without the class (clears out cached temp files I assume)
Copied back over BasePage.vb to App_Code
Reloaded offending page and it loaded without error
I'm assuming that this is the fashion in which to clear out any cached/temp files being referred to in the web. Odd way of doing it, but when you don't have complete control of IIS I guess that's the path one has to take.

ASP.NET CodeFileBaseClass attribute vs. inherit from System.Web.UI.Page

I've just created a base class for my pages by inheriting from System.Web.UI.Page:
public abstract class PageBase : System.Web.UI.Page
{
...
}
When I noticed that you can also declare a base page in an ASP.NET view:
<%# Page Language="C#" CodeFileBaseClass="PageBase.cs" CodeFile="page.aspx.cs"
Inherits="page" %>
Can someone explain what the pros and cons of either method are? When would you use one over the other, or are they both the same? What happens if you used both at the same time?
CodeFileBaseClass, CodeFile, Inherits work together with inheritance, not in place of inheritance.
For example, specifying CodeFile="page.aspx.cs" without page.aspx.cs existing will result in:
Parser Error Message: The file '/page.aspx.cs' does not exist.
Assuming page.aspx.cs exists, specifying CodeFileBaseClass="PageBase.cs" without PageBase.cs existing will result in:
Parser Error Message: Could not load type 'PageBase.cs'.
On the other hand you may inherit from PageBase without specifying the CodeFileBaseClass attribute. This however could result in possible unexpected behaviour when referencing controls on the page from the base class.
To quote from Microsoft's #Page MSDN Documentation:
CodeFileBaseClass
Specifies the type name of a base class for a page and its associated code-behind class. This attribute is optional, but when it is used the
CodeFile attribute must also be present. Use this attribute when you want to implement a shared scenario, where you define common
fields (and optionally, associated events) in a base class to
reference the controls declared in a Web page. Because of the ASP.NET
code generation model, if you defined the fields in a base class
without using this attribute, at compile time new member definitions
would be generated for the controls declared in the Web page (within a
separate partial class stub), and your desired scenario would not
work. But if you use the CodeFileBaseClass attribute to associate
the base class with the page, and you make your partial class (its
name is assigned to the Inherits attribute and its source file is
referenced by the CodeFile attribute) inherit from the base class,
then the fields in the base class will be able to reference the
controls on the page after code generation.

.Net 3.5 NameSpace Problem?

Got an asp.net 3.5 app - compiles fine, but when browse to page I get the error below.
Searches for this all seem to indicate it is a namespace problem, but all of my pages seem to declare the proper namespace.
Any assistance appreciated!
Phil J.
Server Error in '/Internet/bm2/bm2' Application.
Parser Error
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.
Parser Error Message: 'bm2.PolicyLookup' is not allowed here because it does not extend class 'System.Web.UI.Page'.
Source Error:
Line 1: <%# Page Language="vb" AutoEventWireup="false" CodeBehind="PolicyLookup.aspx.vb" Inherits="bm2.PolicyLookup" validateRequest="false" aspcompat="true"%>
Line 2:
Line 3:
Source File: /internet/bm2/bm2/policylookup.aspx Line: 1
=======
The System.Web.UI.Page is indeed inherited from in the code-behind:
Namespace bm2
Public Class PolicyLookup
Inherits System.Web.UI.Page
Protected WithEvents lblResponse As Label
Protected WithEvents btnSubmit As System.Web.UI.WebControls.Button
Dim myModel As New bm2.Model
Dim postData
Public objMessage
If this is a namespace problem (very unlikely), you'll either need to add an #import statement to your page:
<%# Import namespace=”My.Namespace.Where.bm2.PolicyLookup.Exists” %>
or modify your #Page directive to include the fully-qualified class name
....inherits="My.Namespace.bm2.PolicyLookup"
Of course, this only works if bm2.PolicyLookup descends from System.Web.UI.Page.
The #Page attribute
inherits="bm2.PolicyLookup"
tells ASP.NET that the class bm2.PolicyLookup inherits from System.Web.UI.Page, and that it should be instantiated when processing the page to handle on_load(), render(), etc events. However, ASP.NET believes that your class bm2.PolicyLookup does not inherit from System.Web.UI.Page, thus the error.
In order for the webforms engine to be able to process your page, it has to descend from the existing Page class. That class provides all of the event handlers, rendering methods etc. that are required.
Also, a bit of unrelated advice: Visual Studio hides a lot of functionality when working with a VB.Net site. Consider moving to C#, and to a web application project instead of a website project.
I don't know if there is a PolicyLookup class in System.Web (I don't think there is), but I ran into a similar problem when I tried to name a page SiteMap.aspx, since there was a type named SiteMap (though it was in a different namespace, it still seemed to cause a problem). The fix was to either rename completely, or have the code behind class name be prefixed with an underscore (you may have to update the Page directive in your aspx to match the new type name.

ASP.NET Error About "Inherits" attribute, even though it's correct?

Having a really strange issue at the moment with an ASP.NET site I'm currently building.
At the moment, I'm writting the HTML and CSS for a page called Dashboard.aspx. But when I go to view the page in a browser, half the time it throws up this error:
ASPNET: Make sure that the class
defined in this code file matches the
'inherits' attribute, and that it
extends the correct base class (e.g.
Page or UserControl).
I've checked the Inherits attribute on the .aspx page, and it matches the Partial Class held in the VB code of Dashboard.aspx.vb file.
When I refresh, it will always come up with this error. Then after a minute or two, it will load fine with no problems. I had a quick look on Google and I couldn't find an answer to this issue.
I have also changed the name of the partial class (and the content placeholder on the .aspx file, thinking that may have caused a conflict) and nothing I do seems to fix it.
Could anyone shed any light on what's going on?
Thanks,
Michael
Make sure the inherits property in the
aspx page matches the class definition
in the .cs file. […] the inherits
property and the class must have the
same namespace and classname
(From: Inherit problem in asp.net)
Additionally, make sure no other pages (accidentally) try to inherit a class from the same code behind file.

How to make user control partial classes aware of controls declared in the base class?

Do we have to do something special to have ASP.NET partial classes aware of controls that are declared in our user control's base classes? The partial classes keep generating declarations for controls in the base class which mean the controls in the base class get hidden and are null.
The CodeFileBaseClass attribute can be applied to #Page or #Control declarations to make the ASP.NET runtime aware of any controls declared in your base class.
MSDN describes it as follows:
Specifies the type name of a base class for a page and its associated
code-behind class.
This attribute is
optional, but when it is used the
CodeFile attribute must also be
present. Use this attribute when you
want to implement a shared scenario,
where you define common fields (and
optionally, associated events) in a
base class to reference the controls
declared in a Web page. Because of the
ASP.NET code generation model, if you
defined the fields in a base class
without using this attribute, at
compile time new member definitions
would be generated for the controls
declared in the Web page (within a
separate partial class stub), and your
desired scenario would not work. But
if you use the CodeFileBaseClass
attribute to associate the base class
with the page, and you make your
partial class (its name is assigned to
the Inherits attribute and its source
file is referenced by the CodeFile
attribute) inherit from the base
class, then the fields in the base
class will be able to reference the
controls on the page after code
generation.

Resources