Menu Header User Control

Discussion in 'ASP .Net Web Controls' started by AC, Nov 11, 2003.

  1. AC

    AC Guest

    I'm sure this must come up often but I can't find info on
    it..

    I've built a web usercontrol for my sites menu bar... I
    use styles for highlighting text and all that neat stuff..

    Problem: I drag this control on to all my pages, well how
    then do I indicate or highlight the menu item the user is
    on? I cant use any css style like active, visited, etc.
    because that won't work. i.e. - I want the 'Home' label
    to be highlighted if they're on the home page but a user
    control has no idea what page it is currently on...

    thanks
    AC, Nov 11, 2003
    #1
    1. Advertising

  2. AC

    Oliver Guest

    On the page load of each page, you need to get a handle on the usercontrol
    and change its image or style to the highlight style that is appropriate.

    Is that what you mean?


    "AC" <> wrote in message
    news:0e5f01c3a860$696947e0$...
    > I'm sure this must come up often but I can't find info on
    > it..
    >
    > I've built a web usercontrol for my sites menu bar... I
    > use styles for highlighting text and all that neat stuff..
    >
    > Problem: I drag this control on to all my pages, well how
    > then do I indicate or highlight the menu item the user is
    > on? I cant use any css style like active, visited, etc.
    > because that won't work. i.e. - I want the 'Home' label
    > to be highlighted if they're on the home page but a user
    > control has no idea what page it is currently on...
    >
    > thanks
    Oliver, Nov 12, 2003
    #2
    1. Advertising

  3. AC

    AC Guest

    Yes... but how?
    if I have say five labels and want to highlight the
    appropriate one, how would I do that?
    thanks.

    >-----Original Message-----
    >On the page load of each page, you need to get a handle

    on the usercontrol
    >and change its image or style to the highlight style that

    is appropriate.
    >
    >Is that what you mean?
    >
    >
    >"AC" <> wrote in

    message
    >news:0e5f01c3a860$696947e0$...
    >> I'm sure this must come up often but I can't find info

    on
    >> it..
    >>
    >> I've built a web usercontrol for my sites menu bar... I
    >> use styles for highlighting text and all that neat

    stuff..
    >>
    >> Problem: I drag this control on to all my pages, well

    how
    >> then do I indicate or highlight the menu item the user

    is
    >> on? I cant use any css style like active, visited, etc.
    >> because that won't work. i.e. - I want the 'Home' label
    >> to be highlighted if they're on the home page but a user
    >> control has no idea what page it is currently on...
    >>
    >> thanks

    >
    >
    >.
    >
    AC, Nov 12, 2003
    #3
  4. AC

    Oliver Guest

    Hi nameless person,

    I've put together a simple example that changes the appearance of the user
    control's label and changes the location of an image as the main loads.

    The full code is below, but the key part is this from the current .aspx page
    ( see the comments in line:

    Private Sub Page_Load _
    (ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles MyBase.Load
    ' Let's say this page needs to
    ' change Label2 of the user control
    ' and turn the image to French
    Dim navcntrl As UserControl
    Dim navlabel As Label
    Dim navimgbtn As ImageButton
    ' Get an instance of the user control called Nav1
    ' by finding it inside the current page
    navcntrl = Page.FindControl("Nav1")
    ' Now we have an instance of the user control
    ' find the label inside it using FindControl
    navlabel = navcntrl.FindControl("label2")
    ' Change the color of the label that we just found
    navlabel.BackColor = Color.ForestGreen
    ' Likewise get an instance of the image button
    ' by finding it within the user control
    navimgbtn = navcntrl.FindControl("ImageButton1")
    ' Change the URL of the image control
    navimgbtn.ImageUrl = "http://www.gc.ca/images/francaisbt.gif"
    End Sub

    Does this help?


    '---------------------------------------------

    ' usenav.aspx
    <%@ Register TagPrefix="uc1" TagName="nav" Src="nav.ascx" %>
    <%@ Page Language="vb" AutoEventWireup="false" Codebehind="usenav.aspx.vb"
    Inherits="p4320work.usenav"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <title>usenav</title>
    <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema"
    content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="FlowLayout">
    <form id="Form1" method="post" runat="server">
    <uc1:nav id="Nav1" runat="server"></uc1:nav>
    </form>
    </body>
    </HTML>

    'usenav.aspx.vb
    Public Class usenav
    Inherits System.Web.UI.Page

    #Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub
    InitializeComponent()

    End Sub

    'NOTE: The following placeholder declaration is required by the Web Form
    Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Init
    'CODEGEN: This method call is required by the Web Form Designer
    'Do not modify it using the code editor.
    InitializeComponent()
    End Sub

    #End Region

    Private Sub Page_Load _
    (ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles MyBase.Load
    ' Let's say this page needs to
    ' change Label2 of the user control
    ' and turn the image to French
    Dim navcntrl As UserControl
    Dim navlabel As Label
    Dim navimgbtn As ImageButton
    navcntrl = Page.FindControl("Nav1")
    navlabel = navcntrl.FindControl("label2")
    navlabel.BackColor = Color.ForestGreen
    navimgbtn = navcntrl.FindControl("ImageButton1")
    navimgbtn.ImageUrl = "http://www.gc.ca/images/francaisbt.gif"
    End Sub

    End Class


    ' nav.ascx
    <%@ Control Language="vb" AutoEventWireup="false" Codebehind="nav.ascx.vb"
    Inherits="p4320work.nav"
    TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
    <P>
    <asp:Label id="Label1" runat="server" BackColor="#FFFFC0">One</asp:Label>
    <asp:Label id="Label2" runat="server" BackColor="#FFFFC0">Two</asp:Label>
    <asp:Label id="Label3" runat="server" BackColor="#FFFFC0">Four</asp:Label>
    <asp:Label id="Label4" runat="server" BackColor="#FFFFC0">One</asp:Label>
    <asp:Label id="Label5" runat="server"
    BackColor="#FFFFC0">Five</asp:Label></P>
    <P>
    <asp:ImageButton id="ImageButton1" runat="server"
    ImageUrl="http://www.gc.ca/images/englishbt.gif"></asp:ImageButton></P>

    'nav.ascx.vb

    Public Class nav
    Inherits System.Web.UI.UserControl

    #Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub
    InitializeComponent()

    End Sub
    Protected WithEvents Label1 As System.Web.UI.WebControls.Label
    Protected WithEvents Label2 As System.Web.UI.WebControls.Label
    Protected WithEvents Label3 As System.Web.UI.WebControls.Label
    Protected WithEvents Label4 As System.Web.UI.WebControls.Label
    Protected WithEvents Label5 As System.Web.UI.WebControls.Label
    Protected WithEvents ImageButton1 As
    System.Web.UI.WebControls.ImageButton

    'NOTE: The following placeholder declaration is required by the Web Form
    Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Init
    'CODEGEN: This method call is required by the Web Form Designer
    'Do not modify it using the code editor.
    InitializeComponent()
    End Sub

    #End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here
    End Sub

    Private Sub ImageButton1_Click(ByVal sender As System.Object, ByVal e As
    System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
    Label4.Text = "Changed this!"
    End Sub
    End Class


    "AC" <> wrote in message
    news:002801c3a930$125328b0$...
    > Yes... but how?
    > if I have say five labels and want to highlight the
    > appropriate one, how would I do that?
    > thanks.
    >
    > >-----Original Message-----
    > >On the page load of each page, you need to get a handle

    > on the usercontrol
    > >and change its image or style to the highlight style that

    > is appropriate.
    > >
    > >Is that what you mean?
    > >
    > >
    > >"AC" <> wrote in

    > message
    > >news:0e5f01c3a860$696947e0$...
    > >> I'm sure this must come up often but I can't find info

    > on
    > >> it..
    > >>
    > >> I've built a web usercontrol for my sites menu bar... I
    > >> use styles for highlighting text and all that neat

    > stuff..
    > >>
    > >> Problem: I drag this control on to all my pages, well

    > how
    > >> then do I indicate or highlight the menu item the user

    > is
    > >> on? I cant use any css style like active, visited, etc.
    > >> because that won't work. i.e. - I want the 'Home' label
    > >> to be highlighted if they're on the home page but a user
    > >> control has no idea what page it is currently on...
    > >>
    > >> thanks

    > >
    > >
    > >.
    > >
    Oliver, Nov 13, 2003
    #4
  5. AC

    AC Guest

    That does help, I will give that a shot - thank you.

    Anthony

    >-----Original Message-----
    >Hi nameless person,
    >
    >I've put together a simple example that changes the

    appearance of the user
    >control's label and changes the location of an image as

    the main loads.
    >
    >The full code is below, but the key part is this from the

    current .aspx page
    >( see the comments in line:
    >
    > Private Sub Page_Load _
    > (ByVal sender As System.Object, _
    > ByVal e As System.EventArgs) _
    > Handles MyBase.Load
    > ' Let's say this page needs to
    > ' change Label2 of the user control
    > ' and turn the image to French
    > Dim navcntrl As UserControl
    > Dim navlabel As Label
    > Dim navimgbtn As ImageButton
    > ' Get an instance of the user control called Nav1
    > ' by finding it inside the current page
    > navcntrl = Page.FindControl("Nav1")
    > ' Now we have an instance of the user control
    > ' find the label inside it using FindControl
    > navlabel = navcntrl.FindControl("label2")
    > ' Change the color of the label that we just found
    > navlabel.BackColor = Color.ForestGreen
    > ' Likewise get an instance of the image button
    > ' by finding it within the user control
    > navimgbtn = navcntrl.FindControl("ImageButton1")
    > ' Change the URL of the image control
    > navimgbtn.ImageUrl

    = "http://www.gc.ca/images/francaisbt.gif"
    > End Sub
    >
    >Does this help?
    >
    >
    >'---------------------------------------------
    >
    >' usenav.aspx
    ><%@ Register TagPrefix="uc1" TagName="nav" Src="nav.ascx"

    %>
    ><%@ Page Language="vb" AutoEventWireup="false"

    Codebehind="usenav.aspx.vb"
    >Inherits="p4320work.usenav"%>
    ><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0

    Transitional//EN">
    ><HTML>
    > <HEAD>
    > <title>usenav</title>
    > <meta name="GENERATOR" content="Microsoft Visual

    Studio .NET 7.1">
    > <meta name="CODE_LANGUAGE" content="Visual Basic .NET

    7.1">
    > <meta name="vs_defaultClientScript"

    content="JavaScript">
    > <meta name="vs_targetSchema"
    >content="http://schemas.microsoft.com/intellisense/ie5">
    > </HEAD>
    > <body MS_POSITIONING="FlowLayout">
    > <form id="Form1" method="post" runat="server">
    > <uc1:nav id="Nav1" runat="server"></uc1:nav>
    > </form>
    > </body>
    ></HTML>
    >
    >'usenav.aspx.vb
    >Public Class usenav
    > Inherits System.Web.UI.Page
    >
    >#Region " Web Form Designer Generated Code "
    >
    > 'This call is required by the Web Form Designer.
    > <System.Diagnostics.DebuggerStepThrough()> Private Sub
    >InitializeComponent()
    >
    > End Sub
    >
    > 'NOTE: The following placeholder declaration is

    required by the Web Form
    >Designer.
    > 'Do not delete or move it.
    > Private designerPlaceholderDeclaration As

    System.Object
    >
    > Private Sub Page_Init(ByVal sender As System.Object,

    ByVal e As
    >System.EventArgs) Handles MyBase.Init
    > 'CODEGEN: This method call is required by the Web

    Form Designer
    > 'Do not modify it using the code editor.
    > InitializeComponent()
    > End Sub
    >
    >#End Region
    >
    > Private Sub Page_Load _
    > (ByVal sender As System.Object, _
    > ByVal e As System.EventArgs) _
    > Handles MyBase.Load
    > ' Let's say this page needs to
    > ' change Label2 of the user control
    > ' and turn the image to French
    > Dim navcntrl As UserControl
    > Dim navlabel As Label
    > Dim navimgbtn As ImageButton
    > navcntrl = Page.FindControl("Nav1")
    > navlabel = navcntrl.FindControl("label2")
    > navlabel.BackColor = Color.ForestGreen
    > navimgbtn = navcntrl.FindControl("ImageButton1")
    > navimgbtn.ImageUrl

    = "http://www.gc.ca/images/francaisbt.gif"
    > End Sub
    >
    >End Class
    >
    >
    >' nav.ascx
    ><%@ Control Language="vb" AutoEventWireup="false"

    Codebehind="nav.ascx.vb"
    >Inherits="p4320work.nav"
    >TargetSchema="http://schemas.microsoft.com/intellisense/ie

    5" %>
    ><P>
    > <asp:Label id="Label1" runat="server"

    BackColor="#FFFFC0">One</asp:Label>
    > <asp:Label id="Label2" runat="server"

    BackColor="#FFFFC0">Two</asp:Label>
    > <asp:Label id="Label3" runat="server"

    BackColor="#FFFFC0">Four</asp:Label>
    > <asp:Label id="Label4" runat="server"

    BackColor="#FFFFC0">One</asp:Label>
    > <asp:Label id="Label5" runat="server"
    >BackColor="#FFFFC0">Five</asp:Label></P>
    ><P>
    > <asp:ImageButton id="ImageButton1" runat="server"
    >ImageUrl="http://www.gc.ca/images/englishbt.gif"></asp:Ima

    geButton></P>
    >
    >'nav.ascx.vb
    >
    >Public Class nav
    > Inherits System.Web.UI.UserControl
    >
    >#Region " Web Form Designer Generated Code "
    >
    > 'This call is required by the Web Form Designer.
    > <System.Diagnostics.DebuggerStepThrough()> Private Sub
    >InitializeComponent()
    >
    > End Sub
    > Protected WithEvents Label1 As

    System.Web.UI.WebControls.Label
    > Protected WithEvents Label2 As

    System.Web.UI.WebControls.Label
    > Protected WithEvents Label3 As

    System.Web.UI.WebControls.Label
    > Protected WithEvents Label4 As

    System.Web.UI.WebControls.Label
    > Protected WithEvents Label5 As

    System.Web.UI.WebControls.Label
    > Protected WithEvents ImageButton1 As
    >System.Web.UI.WebControls.ImageButton
    >
    > 'NOTE: The following placeholder declaration is

    required by the Web Form
    >Designer.
    > 'Do not delete or move it.
    > Private designerPlaceholderDeclaration As

    System.Object
    >
    > Private Sub Page_Init(ByVal sender As System.Object,

    ByVal e As
    >System.EventArgs) Handles MyBase.Init
    > 'CODEGEN: This method call is required by the Web

    Form Designer
    > 'Do not modify it using the code editor.
    > InitializeComponent()
    > End Sub
    >
    >#End Region
    >
    > Private Sub Page_Load(ByVal sender As System.Object,

    ByVal e As
    >System.EventArgs) Handles MyBase.Load
    > 'Put user code to initialize the page here
    > End Sub
    >
    > Private Sub ImageButton1_Click(ByVal sender As

    System.Object, ByVal e As
    >System.Web.UI.ImageClickEventArgs) Handles

    ImageButton1.Click
    > Label4.Text = "Changed this!"
    > End Sub
    >End Class
    >
    >
    >"AC" <> wrote in

    message
    >news:002801c3a930$125328b0$...
    >> Yes... but how?
    >> if I have say five labels and want to highlight the
    >> appropriate one, how would I do that?
    >> thanks.
    >>
    >> >-----Original Message-----
    >> >On the page load of each page, you need to get a handle

    >> on the usercontrol
    >> >and change its image or style to the highlight style

    that
    >> is appropriate.
    >> >
    >> >Is that what you mean?
    >> >
    >> >
    >> >"AC" <> wrote in

    >> message
    >> >news:0e5f01c3a860$696947e0$...
    >> >> I'm sure this must come up often but I can't find

    info
    >> on
    >> >> it..
    >> >>
    >> >> I've built a web usercontrol for my sites menu

    bar... I
    >> >> use styles for highlighting text and all that neat

    >> stuff..
    >> >>
    >> >> Problem: I drag this control on to all my pages, well

    >> how
    >> >> then do I indicate or highlight the menu item the

    user
    >> is
    >> >> on? I cant use any css style like active, visited,

    etc.
    >> >> because that won't work. i.e. - I want the 'Home'

    label
    >> >> to be highlighted if they're on the home page but a

    user
    >> >> control has no idea what page it is currently on...
    >> >>
    >> >> thanks
    >> >
    >> >
    >> >.
    >> >

    >
    >
    >.
    >
    AC, Nov 13, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mad Scientist Jr
    Replies:
    0
    Views:
    616
    Mad Scientist Jr
    Mar 22, 2006
  2. Replies:
    0
    Views:
    436
  3. mlt
    Replies:
    2
    Views:
    804
    Jean-Marc Bourguet
    Jan 31, 2009
  4. Jonathan Wood

    User Control to Control Other User Control

    Jonathan Wood, Jan 24, 2010, in forum: ASP .Net
    Replies:
    4
    Views:
    518
    Jonathan Wood
    Feb 2, 2010
  5. naijacoder naijacoder

    How to create dynamic menu with user roles (Treeview menu)

    naijacoder naijacoder, Sep 27, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    335
    naijacoder naijacoder
    Sep 27, 2004
Loading...

Share This Page