I have generate a ASP.NET page with a Codebehind but i want to create a file with common method that i must make in many page of my website.
I create a new file class .vb, but how can I using the public function that I have insert in??
Thanks a lot for your help.

Dani AI

Generated

Two common, safe ways to share code across ASP.NET pages are: 1) make a base page class (useful when pages must share lifecycle logic or protected helpers), or 2) put stateless helpers in a module/class with Shared methods (simpler and less intrusive). 's base-class idea works when instance/page behavior is needed; a Shared helper module is usually the better choice for general utility methods. 's follow-up indicates the inheritance route solved the original problem — the examples below make both approaches explicit and list the usual deployment gotchas.

' App_Code/Utilities.vb
Public Module Utilities
    Public Function NormalizePhoneNumber(input As String) As String
        If String.IsNullOrEmpty(input) Then Return String.Empty
        Dim sb As New System.Text.StringBuilder()
        For Each ch As Char In input
            If Char.IsDigit(ch) Then sb.Append(ch)
        Next
        Return sb.ToString()
    End Function
End Module

' Usage in a page codebehind
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    Dim digits As String = Utilities.NormalizePhoneNumber("(555) 123-4567")
End Sub
' App_Code/BasePage.vb
Public Class BasePage
    Inherits System.Web.UI.Page

    Protected Function FriendlyDate(d As DateTime) As String
        Return d.ToString("yyyy-MM-dd")
    End Function
End Class

' In a page codebehind that inherits BasePage:
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    Dim s As String = FriendlyDate(DateTime.Now)  ' direct call to base-class method
End Sub

Troubleshooting / deployment notes: put utility files in App_Code for Web Site projects or include them in the Web Application project with Build Action = Compile; ensure namespaces match or add Imports; mark methods Protected if pages should call instance methods, or Shared/Module when no instance is required; check the aspx Page directive Inherits value (Web Application needs fully qualified name). Common errors like "Type not defined" usually mean the file isn't compiled into the project, the namespace is wrong, or the method is Private. Prefer composition (Shared helpers or service classes) for general utilities and use a base page only when pages must share lifecycle behavior.

Here is an example using a class called MyClass in Page1.aspx

In you class file:

Public Class MyClass
    Inherits System.Web.UI.Page

In your code behind page:

Public Class Page1
    Inherits MyClass

So, you are replacing "Inherits System.Web.UI.Page" in your codebehind pages with the name of your class.

-sypher

Thanks for you help but I don't know how it is possible to call the metod when I have defined the Inherits, can you post me an complete example??
I prove many time but I don't found an example to refer for resolve this problem.

Thanks a lot

I found the correct way to call the method that is included with the inheriths.
Thanks a lot for your help.

:) :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.