• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 189
  • Last Modified:

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

  • 3
  • 3
1 Solution
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.
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

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
cdemott33Author Commented:
I moved it to a master page.  Thanks!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now