Can I move my code from code behind to a class?

Hi all - I have a subroutine in my code behind that creates the main navigation tabs.  I have this block of code on each code behind page of my site.  It's not a large block of code (see code attached) but if possible I'd like to move it to it's own class?  Is this possible?  If possible how could I move this to a class?  What changes would I have to make to my code to get it to work on all the pages of my site?  Should I just use a MasterPage?
Public Sub LoadMainNav(ByVal SelectedTab As String)

        Dim dt As DataTable = categoryLogic.GetAllCategories()

        If dt.Rows.Count > 0 Then
            For Each row As DataRow In dt.Rows
                ' Create a tab using Panels
                Dim navTab As New Panel()

                ' Determine if this is the active tab
                If SelectedTab = row("CategoryID").ToString Then
                    navTab.CssClass = "activeTab"
                    navTab.CssClass = "inactiveTab"
                End If

                ' Create a link button
                Dim navLink As New LinkButton()
                navLink.CssClass = "TabLinks"
                navLink.Text = row("CategoryName").ToString
                navLink.CommandName = "category"
                navLink.CommandArgument = row("CategoryID").ToString
                AddHandler navLink.Command, AddressOf LoadGroupsTypes

                ' Add the Link button to the tab

                ' Add the tab to the page
        End If

        ' Create the Cart tab
        Dim cartTab As New Panel()
        cartTab.CssClass = "cartTab"

        ' Create the hyperlink
        Dim cartLink As New HyperLink()
        cartLink.CssClass = "CartLinks"
        cartLink.NavigateUrl = "Cart.aspx"
        cartLink.Text = "Your Cart"

        ' Add the Hyperlink to the tab container

        ' Add Cart tab to the Page

    End Sub

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it is possible, as long as all the objects are public and can be accessed through public class
But in order to really help you, I am gonna need more information on what you are trying to do and what are categoryLogic and navTab
cdemott33Author Commented:
The categoryLogic class accesses my DAL and returns the names and id of my tabs from the database.  

I have several pages thoughtout my web that have this subroutine.  All it does is create the tabs (the main navigation) across the top of the page.  Right now that subroutine (above) is on every single aspx.vb (Code Behine) page.  I was just sort of hoping that, rather than having the subroutine on every single page I could just put it in a class file and call it.

I'm thinking it might be better/ easier to use a MasterPage but I'm not sure.
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

I believe you can just move the code to a public class.  And try running it. If it does not work right away, make sure your tabs are declared public so they could be accessed from within the class
cdemott33Author Commented:
I'm confused about the "Public" thing you mention.  Maybe you can help me understand.  My aspx page uses place holders (see ASPX code).  My code behind just load my tabs into the "PlaceHolderMainNav" place holder.  How would I determine if they were public or not?
<%@ Page Title="" Language="VB" MasterPageFile="~/dealer.master" AutoEventWireup="false" CodeFile="Inventory.aspx.vb" Inherits="Inventory" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <div id="MainNav" class="mainTabContainer">
        <asp:PlaceHolder ID="PlaceHolderMainNav" runat="server"></asp:PlaceHolder>
    <div class="clear"></div>


        <div id="SubMenu" class="subTabContainer">
            <asp:PlaceHolder ID="PlaceHolderSubNav" runat="server"></asp:PlaceHolder>

        <div id="ContentArea" class="contentContainer">
            <asp:PlaceHolder ID="PlaceHolderContentArea" runat="server"></asp:PlaceHolder>

        <div class="clear"></div>


Open in new window

Other options:
- move it to a Master page
- move it to a Module (which is something like a container for public functions

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cdemott33Author Commented:
I moved it to a master page.  Thanks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.