Menu Header User Control

A

AC

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
 
O

Oliver

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?
 
A

AC

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

Oliver

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
 
A

AC

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 said:
<asp:Label id="Label2" runat="server"
BackColor="#FFFFC0">Two said:
<asp:Label id="Label3" runat="server"
BackColor="#FFFFC0">Four said:
<asp:Label id="Label4" runat="server"
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top