Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Using the Web.UI.Page class within another class

Author  Topic 

Billkamm
Posting Yak Master

124 Posts

Posted - 2006-02-09 : 14:11:47
I have several webpages that use the same code in the codebehind to genereate parts of the web page. I would like to put this code into another class or module and share it among all of the pages in my class.

The problem is that hese procedures use methods such Response.Write and Application() and some of them need to be passed various controls from the page.

Is there a way that I can separate out this code from the code behind? I've tried Imports System.Web.UI.Page, but that apparently didn't work at all lol.

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2006-02-09 : 14:25:36
You are after something called "Inheritance". You define a base page class and put your methods on that page, and in your other pages (which are all classes) you inherit from that base class. In fact, every web page you write inherits from System.Web.UI.Page. This is a fairly advanced topic on OOP but something worth reading about.

For example:

1. Create a new class file in your project. Call it "basePage.vb"

2. on that file, put the following code:


Public class BasePage
inherits System.Web.UI.Page

Protected Function SayHello() as String
Return "Hello"
End Function

end class


3. Now, create a new WebForm page. Go to the code behind, and note at the top it says the class inherits from System.Web.UI.Page. Replace this to read

Inherits BasePage


4. Once that is done, you will note that the method SayHello() is now available to your page! Your new webform is a class that inherits from BasePage, so all methods and properties that the base page defines as "Public" or "Protected" are available to the class that is derived from the base. Since BasePage inherits from System.Web.UI.Page, all of those methods and properties are available as well.
Go to Top of Page
   

- Advertisement -