• 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"
                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

0
cdemott33
Asked:
cdemott33
  • 3
  • 3
1 Solution
 
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
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.

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