Access function member of base page class? - asp.net

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.

Related

Designer page is prepending Global to custom controls definition namespace

I've got a custom control defined in my App_Code folder. When I add it to a page, I get the error Type 'Global.XXX.xxx.MyControl' is not defined
Class:
Namespace XXX.xxx
Public Class MyControl
Inherits TextBox
...code...
End Class
End Namespace
Page Useage:
<%# Register TagPrefix="xxx" Namespace="XXX.xxx" %>
...
<xxx:MyControl runat="server" id="StartTime" />
Designer:
Protected WithEvents StartTime As Global.XXX.xxx.MyControl
So what's happening is Global is being prepended to the namespace in the designer file. For some reason, the IDE thinks my custom control is living in the global namespace, but as far as I can see from searching the Object Browser, it isn't. Manually going in and deleting the prepended Global from the designer file allows me to build the page successfully, view it and test the functionality of my control, however this is far from an ideal solution, as the designer file is regenerated every time you make a change to the corresponding page.
Any ideas as to why it thinks my custom control, living in the App_Code folder, is in the Global namespace? Any suggestions on a better fix than the manual one?
EDIT: Perhaps a better question would be how to get that namespace into the Global namespace, so that when it creates the designer file, it's correct?
The question Type or namespace could not be found from App_code folder may be what you want.
Either put your class in a different folder or change the build settings as per http://vishaljoshi.blogspot.co.uk/2009/07/appcode-folder-doesnt-work-with-web.html

ASPX Page 'Could not load type' Error

I have an .aspx page, we’ll call it Data.aspx, and it is going to use an ajax request to retrieve a response from a function called GenerateDocument in another .aspx page in the project, we’ll call this Document.aspx. It submits its ajax request, but the request fails, and I’m trying to decipher if it has to do with my page directives on Document.aspx. The entirety of the content on Document.aspx is this (it is not meant to generate a page, only generate documents based on data passed to it):
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="Document.aspx.vb" Inherits="NameSpace.Document" %>
In Document.aspx.vb, we have:
Public Class Document
Inherits Generic.WebPage
<System.Web.Services.WebMethod()> _
Public Shared Function GenerateDocument
‘Process data and return as document
End Function
End Class
(There's more than just this; there's also a page load function, etc, but this is the relevant function I'm trying to hit.)
With this configuration, the error we get is complaining about ‘Could not load type NameSpace.Document’
I didn't think I should have to use a namespace that the class does not seem to be contained within, so I tried just saying Inherits="Document", but with the same error loading type message coming back.
I am building the project after I make fixes and getting no build errors, but if I try and change the CodeBehind to a CodeFile attribute, I get runtime compilation errors instead of type errors (I am tracking this through Fiddler).
This seems like it's probably an obvious issue where the function is just not being hit, but I can't seem to parse it. Any help? Thanks.
I figured out the issue. Thanks to all those who responded!
The problem was that while the files were all in the proper directory, one was not added to the project within the solution, and despite the references being in order (Inherits I left pointing to a namespace the class inherited from, plus the class), the program was not finding them at run time. This became apparent when I tried using CodeFile and got compilation errors and realized that it simply did not know what class to point to.
TL;DR - I added the .aspx file to the project and the whole thing worked fine.

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.

Resources