M
Murray Gill
Our current solution has a number of ASP.NET pages with very similar functionality. We would like to move the common functions into a base class that inherits from System.Web.UI.Page, and then force the child classes to override certain functions of that class.
The best way to do this is to define the base class as "MustInherit" and put "MustOverride" on the functions that must be overridden.
However, when we do this, whenever we attempt to open the aspx that inherits from the base class, we recieve the error:
"The file could not be opened in the Web Form designer. Please correct the following error and try again:
Type Abstract
Make sure all the classes used in the page are built or referenced in the project. Click Help for more information."
We have noticed similar behaviour when attempting to inherit a UserControl from an abstract class.
Two questions:
1) Is there any way of having a code-behind class inherit from an abstract (mustinherit) class without seeing the above behaviour in the designer?
2) Will the above behaviour impact the solution at all? We are still able to compile, the only effect we have noticed is that we cannot open the web form in the .NET designer.
Example Code:
PARENT CLASS:
Public MustInherit Class ParentClass
Inherits System.Web.UI.Page
Protected MustOverride Function GetMessageFromChild()
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Response.Write(Me.GetMessageFromChild)
End Sub
End Class
CHILD CLASS:
Public Class ChildClass
Inherits ParentClass
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration 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
'Put user code to initialize the page here
End Sub
Protected Overrides Function GetMessageFromChild() As Object
Return "Here is a message from a child page"
End Function
End Class
Web Form:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="ChildClass.aspx.vb" Inherits="WebApplication1.ChildClass"%>
<html>
<body>
<form id="Form1" method="post" runat="server">
</form>
</body>
</html>
The best way to do this is to define the base class as "MustInherit" and put "MustOverride" on the functions that must be overridden.
However, when we do this, whenever we attempt to open the aspx that inherits from the base class, we recieve the error:
"The file could not be opened in the Web Form designer. Please correct the following error and try again:
Type Abstract
Make sure all the classes used in the page are built or referenced in the project. Click Help for more information."
We have noticed similar behaviour when attempting to inherit a UserControl from an abstract class.
Two questions:
1) Is there any way of having a code-behind class inherit from an abstract (mustinherit) class without seeing the above behaviour in the designer?
2) Will the above behaviour impact the solution at all? We are still able to compile, the only effect we have noticed is that we cannot open the web form in the .NET designer.
Example Code:
PARENT CLASS:
Public MustInherit Class ParentClass
Inherits System.Web.UI.Page
Protected MustOverride Function GetMessageFromChild()
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Response.Write(Me.GetMessageFromChild)
End Sub
End Class
CHILD CLASS:
Public Class ChildClass
Inherits ParentClass
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration 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
'Put user code to initialize the page here
End Sub
Protected Overrides Function GetMessageFromChild() As Object
Return "Here is a message from a child page"
End Function
End Class
Web Form:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="ChildClass.aspx.vb" Inherits="WebApplication1.ChildClass"%>
<html>
<body>
<form id="Form1" method="post" runat="server">
</form>
</body>
</html>