fwsteal
asked on
how to use base page
I created two separate VS2003 projects - web app with asp.net in vb and a class library. My goal is to create one dll that will contain all common code and have this code used by the web app.
1. class library: MyBase
'contents of BasePage.vb
Imports System
Imports System.Web.UI 'Error - Namespace or type 'UI' for the Imports 'System.Web.UI' cannot be found.
Namespace BasePageVB
Public Class BasePage
Inherits Page 'Error - Type 'Page' is not defined.
End Class
End Namespace
1.1. Why the errors?
1.2. Once the errors are corrected, can I put the dll on the web server and use it in the web app?
ex: wwwroot/bin/MyBase.dll
2. asp.net/vb: testbase
Add a reference: in the dialog box, click .Net and browse to find the MyBase.dll. What I'd like to do is inherit the base page as opposed to the System.Web.UI.Page:
'contents of default.vb
Imports System
Public Class WebForm1
'Inherits System.Web.UI.Page
Inherits [MyBase].BasePageVB
2.1 why do i get errors?
what is the best way of doing this?
1. class library: MyBase
'contents of BasePage.vb
Imports System
Imports System.Web.UI 'Error - Namespace or type 'UI' for the Imports 'System.Web.UI' cannot be found.
Namespace BasePageVB
Public Class BasePage
Inherits Page 'Error - Type 'Page' is not defined.
End Class
End Namespace
1.1. Why the errors?
1.2. Once the errors are corrected, can I put the dll on the web server and use it in the web app?
ex: wwwroot/bin/MyBase.dll
2. asp.net/vb: testbase
Add a reference: in the dialog box, click .Net and browse to find the MyBase.dll. What I'd like to do is inherit the base page as opposed to the System.Web.UI.Page:
'contents of default.vb
Imports System
Public Class WebForm1
'Inherits System.Web.UI.Page
Inherits [MyBase].BasePageVB
2.1 why do i get errors?
what is the best way of doing this?
It certainly sounds like you are missing a reference to one or more classes of the .Net Framework. Make sure you have a reference to the System and System.Web classes in your references section of your solution explorer. Also, a good thing to do in the code above is to include the namespace of the class you are using. This can tell you at which point your reference(s) have failed.
EXAMPLE:
Imports System
Imports System.Web.UI
Namespace BasePageVB
Public Class BasePage
Inherits System.Web.UI.Page
End Class
End Namespace
Also, your page may not be in the same Namespace as your base class...
EXAMPLE:
Imports System
Namespace BasePageVB
Public Class WebForm1
Inherits BasePageVB.BasePage
End Class
End Namespace
OR:
Imports System
Public Class WebForm1
Inherits BasePageVB.BasePage
End Class
EXAMPLE:
Imports System
Imports System.Web.UI
Namespace BasePageVB
Public Class BasePage
Inherits System.Web.UI.Page
End Class
End Namespace
Also, your page may not be in the same Namespace as your base class...
EXAMPLE:
Imports System
Namespace BasePageVB
Public Class WebForm1
Inherits BasePageVB.BasePage
End Class
End Namespace
OR:
Imports System
Public Class WebForm1
Inherits BasePageVB.BasePage
End Class
ASKER
class library - I added a reference to system.web and it cleaned up all the errors; then recompiled the code.
In the web app I get:
Imports System
Public Class WebForm1
'Inherits System.Web.UI.Page
Inherits [MyBase].BasePageVB
error: type expected.
the dll is added as a reference
In the web app I get:
Imports System
Public Class WebForm1
'Inherits System.Web.UI.Page
Inherits [MyBase].BasePageVB
error: type expected.
the dll is added as a reference
Are you getting a compile error for the inherits line? I would suspect that you should. Your inherits statement is attempting to inherit a class, not a type (as expected). Try this:
Public Class WebForm1
'Inherits System.Web.UI.Page
Inherits BasePageVB.BasePage
Public Class WebForm1
'Inherits System.Web.UI.Page
Inherits BasePageVB.BasePage
ASKER
Here is my error by doing that: Type 'BasePageVB.BasePage' is not defined.
Wrap your class in the same namespace as the base page. It is in a different namespace, so it cannot see it.
FROM THE PREVIOUS EXAMPLE:
Namespace BasePageVB
Public Class WebForm1
Inherits BasePageVB.BasePage
End Class
End Namespace
FROM THE PREVIOUS EXAMPLE:
Namespace BasePageVB
Public Class WebForm1
Inherits BasePageVB.BasePage
End Class
End Namespace
did u try this?
Imports BasePageVB.BasePage ( or give ur dll namespace name here)
Public Class WebForm1
'Inherits System.Web.UI.Page
Inherits BasePageVB.BasePage
Rana
Imports BasePageVB.BasePage ( or give ur dll namespace name here)
Public Class WebForm1
'Inherits System.Web.UI.Page
Inherits BasePageVB.BasePage
Rana
ASKER
I tried both and get the following errors
Imports System
Namespace BasePageVB
Public Class WebForm1
Inherits [MyBase].BasePageVB 'error - type expected.
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
End Class
End Namespace
Imports System
Imports [MyBase].BasePageVB
Public Class WebForm1
Inherits [MyBase].BasePageVB 'error - type expected.
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
End Class
Imports System
Namespace BasePageVB
Public Class WebForm1
Inherits [MyBase].BasePageVB 'error - type expected.
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
End Class
End Namespace
Imports System
Imports [MyBase].BasePageVB
Public Class WebForm1
Inherits [MyBase].BasePageVB 'error - type expected.
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
End Class
You are still trying to inherit the CLASS and not the TYPE:
Inherits [MyBase].BasePageVB 'error - type expected.
SHOULD BE:
Inherits BasePageVB.BasePage
Inherits [MyBase].BasePageVB 'error - type expected.
SHOULD BE:
Inherits BasePageVB.BasePage
ASKER
Inherits BasePageVB.BasePage
gives me the following error:
type BasePageVB.BasePage is not defined
----------
Imports System
Namespace BasePageVB
Public Class WebForm1
Inherits BasePageVB.BasePage 'Type 'BasePageVB.BasePage' is not defined.
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
End Class
End Namespace
gives me the following error:
type BasePageVB.BasePage is not defined
----------
Imports System
Namespace BasePageVB
Public Class WebForm1
Inherits BasePageVB.BasePage 'Type 'BasePageVB.BasePage' is not defined.
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
End Class
End Namespace
Well, from the snippets you have posted, that looks like it should work. Do you have the namespace defined in your project settings? Also, what happens as you type the inherits line? Does it not lead you to the class/type via intellisense?
ASKER
yes:
Property Page dialog box
Common Properties
Imports - Namespace: BasePageVB is present int he Project imports section
Property Page dialog box
Common Properties
Imports - Namespace: BasePageVB is present int he Project imports section
What about the intellisense? Did that not lead you to the correct path to the type? You can determine that using the Class View window.
ASKER
if i allow intellisense to work by default it writes it as:
Inherits [MyBase].BasePageVB
Inherits [MyBase].BasePageVB
ASKER
this works:
Inherits [MyBase].BasePageVB.BasePa ge
now i need to put something in the base class file; like a string value and present it on the default.aspx file to know whether it works or not
Inherits [MyBase].BasePageVB.BasePa
now i need to put something in the base class file; like a string value and present it on the default.aspx file to know whether it works or not
I am not sure about the [MyBase] part, but if intellisense finds it, then it is right 98% of the time. However, you are indeed referencing the Type now, not the Class by using BasePage. I do something very similar to my applications. It works nicely.
ASKER
MyBase/BasePage.vb - class file
---------
Imports System
Imports System.Web.UI
Namespace BasePageVB
Public Class BasePage
Inherits System.Web.UI.Page
Public name As String
Private doorid As Integer
Public Sub New()
doorid = 0
name = "nothing"
End Sub
Public Property intID() As Integer
Get
intID = doorid
End Get
Set(ByVal Value As Integer)
doorid = Value
End Set
End Property
Public Overrides Sub Dispose()
name = ""
doorid = 0
End Sub
Protected Overrides Sub Finalize()
End Sub
End Class
Module Module1
Sub Main()
Dim dDoor As New BasePage
dDoor.name = "Red Apple Door"
dDoor.intID = 1
dDoor.Dispose()
End Sub
End Module
End Namespace
************************** ********** ********** ******
testbase/WebForm1.aspx.vb - asp.net
have a project reference to class file
Imports System
Namespace BasePageVB
Public Class WebForm1
'Inherits System.Web.UI.Page
Inherits [MyBase].BasePageVB.BasePa ge
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.Debugg erStepThro ugh()> Private Sub InitializeComponent()
End Sub
Protected WithEvents lblProductName As System.Web.UI.WebControls. Label
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclara tion As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lblProductName.Text = name.ToString
End Sub
End Class
End Namespace
************************** ********** ********** ********
Error:
Namespace or type 'BasePageVB' in the project-level Imports 'BasePageVB' cannot be found.
---------
Imports System
Imports System.Web.UI
Namespace BasePageVB
Public Class BasePage
Inherits System.Web.UI.Page
Public name As String
Private doorid As Integer
Public Sub New()
doorid = 0
name = "nothing"
End Sub
Public Property intID() As Integer
Get
intID = doorid
End Get
Set(ByVal Value As Integer)
doorid = Value
End Set
End Property
Public Overrides Sub Dispose()
name = ""
doorid = 0
End Sub
Protected Overrides Sub Finalize()
End Sub
End Class
Module Module1
Sub Main()
Dim dDoor As New BasePage
dDoor.name = "Red Apple Door"
dDoor.intID = 1
dDoor.Dispose()
End Sub
End Module
End Namespace
**************************
testbase/WebForm1.aspx.vb - asp.net
have a project reference to class file
Imports System
Namespace BasePageVB
Public Class WebForm1
'Inherits System.Web.UI.Page
Inherits [MyBase].BasePageVB.BasePa
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.Debugg
End Sub
Protected WithEvents lblProductName As System.Web.UI.WebControls.
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclara
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lblProductName.Text = name.ToString
End Sub
End Class
End Namespace
**************************
Error:
Namespace or type 'BasePageVB' in the project-level Imports 'BasePageVB' cannot be found.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
1.1 Make sure you have the System reference in the project... and then use: Inherits System.Web.UI.Page
1.2 Yes... just add the reference to the dll and you'll can use it