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"
                Else
                    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
                navTab.Controls.Add(navLink)

                ' Add the tab to the page
                PlaceHolderMainNav.Controls.Add(navTab)
            Next
        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
        cartTab.Controls.Add(cartLink)

        ' Add Cart tab to the Page
        PlaceHolderMainNav.Controls.Add(cartTab)

    End Sub

Open in new window

cdemott33Asked:
Who is Participating?

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

x
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.

YZlatCommented:
it is possible, as long as all the objects are public and can be accessed through public class
0
YZlatCommented:
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
0
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.
0
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.

YZlatCommented:
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
0
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>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <div id="MainNav" class="mainTabContainer">
        <asp:PlaceHolder ID="PlaceHolderMainNav" runat="server"></asp:PlaceHolder>
    </div>
    <div class="clear"></div>

    <div>

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

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

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

    </div>
</asp:Content>

Open in new window

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

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!
0
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
ASP.NET

From novice to tech pro — start learning today.