Search code examples
asp.netvb.netclasscode-behind

How to Share Code Between Pages and Classes


This seems like it should be simple, but I cannot figure this out. I'm trying to share code between a Code Behind Page and a Class that inherits the Page class.

An important note is that I need the Class to inherit the Page class, becuase I will be using objects and methods that only (I think) work with/on a Page class. For example, I'll be using the "reponse" and "session" objects. Later I'll be using other page objects and methods (e.g. GridView, Panels, etc...). I'd like to save common Page related code in a shared Class.

Sample code below.

My Test Page. The code behind page is referring to a function that lives in a shared Class. When I load this page it throws an error on the first line referring to the "response" object (says it's not declared and cannot be used in this context).

MyTestPage.aspx.vb:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="MyTestPage.aspx.vb" Inherits="MyTestPage" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>My Test Page</title>
</head>
<body>
    <form id="form1" runat="server" class="container-fluid">
        <p>My Test Page</p>
    </form>
</body>
</html>

MyTestPage.aspx.vb:
Partial Class MyTestPage
    Inherits System.Web.UI.Page

    Dim sMYSHAREDCLASS As New Class1

    Private Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If sMYSHAREDCLASS.MyTestFunction = True Then
            Response.Write("hello")
        End If
    End Sub
End Class

Class. Here's the shared code for the shared Class. I like to use Page objects and methods that I'll be using on multiple pages.

Public Class Class1
    Inherits System.Web.UI.Page

    Function MySharedTestFunction() As Boolean
        Response.Buffer = True
        Session("MySession") = False
        Return (True)
    End Function
End Class

Solution

  • Well, you are creating a new instance of a class.

    It can't inherit other things that don't exist, or that use other classes, including the page class UNLESS you tell it to!!!!

    So, some instance of a class can't out of the blue use/enjoy/touch/use or have a context to the CURRENT page, since what is that CURRENT page if I have 5 pages open in a browser????

    So, you need on creating of that class to ALSO pass the page it supposed to be working on.

    Furthermore, this requirement IS NOT called inheritance, and you don't need, and are not using inheritance here anyway at all!!!@!

    So, your page class can be this:

    Public Class Class1
    
        Dim MyPage As Page
    
        Public Sub New(MePage As Page)
    
            MyPage = MePage
    
        End Sub
    
    
        Public Function MySharedTestFunction() As Boolean
            MyPage.Response.Buffer = True
            MyPage.Session("MySession") = False
            Return (True)
        End Function
    
    
    End Class
    

    And now, your web page code behind will be this:

    Dim cMyPage As New Class1(Me)
    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
    End Sub
    

    So, when we create the "instance" of class1, we also at that time simple pass the current page that we are working on (me).

    So, now, any code, and any place in the web page code behind can go:

      cMyPage.MySharedTestFunction()
    

    So that class does not have to inherit anything at all. All it needs is a working reference to the web page that created the class in the first place.

    So, now, in that class, you can reference and do anything on that page by using MyPage.