I would like to pass a value from menu item to the respected navigation page.
<asp:menu id="myMenu"
staticdisplaylevels="2"
staticsubmenuindent="10"
orientation="Horizontal"
dynamichorizontaloffset="10"
onmenuitemclick="myMenu_MenuItemClick"
target="_blank"
runat="server" ForeColor="White" >
<items>
<asp:menuitem
text="Home"
tooltip="Home">
<asp:menuitem
text="Music"
tooltip="Music">
<asp:menuitem
text="Classical"
tooltip="Classical"/>
<asp:menuitem
text="Rock"
tooltip="Rock"/>
<asp:menuitem
text="Jazz"
tooltip="Jazz"/>
</asp:menuitem>
<asp:menuitem
text="Movies"
tooltip="Movies">
<asp:menuitem
text="Action"
tooltip="Action"/>
<asp:menuitem
text="Drama"
tooltip="Drama"/>
<asp:menuitem
text="Musical"
tooltip="Musical"/>
</asp:menuitem>
</asp:menuitem>
</items>
</asp:menu>
The code to pass the MenuItem Text is as below:
protected void myMenu_MenuItemClick(object sender, EventArgs e)
{
Session["1"] = (sender as MenuItem).Text;
Response.Redirect("~/CategorySearch.aspx?Category=" + Session["1"].ToString());
}
I get a NullReferenceException which is quite interesting because I am just passing the text of the MenuItem.Why is that?
If you have an event handler attached to your menu (or menu item), you can easily cast the sender as a MenuItem like so:
Webforms:
<asp:Menu runat="server" ID="mymenu" OnMenuItemClick="mymenu_MenuItemClick">
<Items>
<asp:MenuItem Text="Home" />
<asp:MenuItem Text="Contact" />
<asp:MenuItem Text="About Us" />
</Items>
</asp:Menu>
Code Behind:
protected void mymenu_MenuItemClick(object sender, MenuEventArgs e)
{
// this is the menu itself, you can iterate the Items collection if you need.
var menu = (sender as Menu);
foreach (MenuItem item in menu.Items)
{
System.Diagnostics.Debug.Print(item.Text);
}
// this is the MenuItem object that was clicked
var clickedMenuItem = e.Item;
// store text value in your session
Session["1"] = e.Item.Text;
// redirect
Response.Redirect("~/CategorySearch.aspx?Category=" + e.Item.Text);
}
Related
I need to make a change to a website's menu system which is written in asp.net and vb.net. I would like to hide the menu item (Value="ProdinTran") from being seen when a user is not logged into the site. Can you show me what I need to add to this code to do this?
Here is the current code:
<%# Master Language="VB" AutoEventWireup="false" CodeFile="Site.Master.vb" Inherits="Site" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head runat="server">
<title></title>
<link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
<asp:ContentPlaceHolder ID="HeadContent" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form runat="server">
<div class="page">
<div class="header">
<div class="title">
<h1>
Portal
</h1>
</div>
<div class="loginDisplay">
<asp:LoginView ID="HeadLoginView" runat="server" EnableViewState="false">
<AnonymousTemplate>
[ Log In ]
</AnonymousTemplate>
<LoggedInTemplate>
Welcome <span class="bold"><asp:LoginName ID="HeadLoginName" runat="server" /> </span>!
[ <asp:LoginStatus ID="HeadLoginStatus" runat="server" LogoutAction="Redirect" LogoutText="Log Out" LogoutPageUrl="~/"/> ]
</LoggedInTemplate>
</asp:LoginView>
</div>
<div class="clear hideSkiplink">
<asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal" DisappearAfter="1000">
<Items>
<asp:MenuItem NavigateUrl="~/Default.aspx" Text="Home">
<asp:MenuItem NavigateUrl="~/Default.aspx" Text="Portal Home Page"
Value="Portal Home Page"></asp:MenuItem>
<asp:MenuItem NavigateUrl="http://www.something.com/index.php?option=com_content&view=article&id=212&Itemid=28" Text="About"
Value="About" Target="_parent"></asp:MenuItem>
<asp:MenuItem NavigateUrl="http://www.something.com"
Text="Web Site"
ToolTip="Home Page" Value="Home" Target="_parent">
</asp:MenuItem>
</asp:MenuItem>
<asp:MenuItem Text="Customer" Value="Customer"
ToolTip="Registered customers" NavigateUrl="~/Customer.aspx?mySelect=0">
<asp:MenuItem NavigateUrl="~/Customer.aspx?mySelect=0"
Text="Customer Home Page" Value="Chp">
</asp:MenuItem>
<asp:MenuItem NavigateUrl="~/Customer.aspx?mySelect=2"
Text="Open Orders"
ToolTip="View your open orders" Value="OpenOrders">
</asp:MenuItem>
<asp:MenuItem NavigateUrl="~/Customer.aspx?mySelect=3"
Text="Canceled Orders"
ToolTip="View your canceled orders" Value="Canceled">
</asp:MenuItem>
<asp:MenuItem NavigateUrl="~/Customer.aspx?mySelect=4"
Text="Shipped Orders"
ToolTip="View your shipped orders" Value="Shipped">
</asp:MenuItem>
<asp:MenuItem Text="Product in Transit" ToolTip="Product in Transit" Value="ProdinTran" NavigateUrl="http://www.google.com">
</asp:MenuItem>
<asp:MenuItem NavigateUrl="~/Customer.aspx?mySelect=5"
Text="Invoices"
ToolTip="View your invoices" Value="Invoices">
</asp:MenuItem>
<asp:MenuItem NavigateUrl="~/Customer.aspx?mySelect=6"
Text="Credit Memos"
ToolTip="View your credit memos" Value="Credits">
</asp:MenuItem>
<asp:MenuItem NavigateUrl="~/Account/ChangePassword.aspx"
Text="Change Password" ToolTip="Change your password."
Value="Change">
</asp:MenuItem>
</asp:MenuItem>
<asp:MenuItem Text="Help & Information"
ToolTip="Holidays, Shipping, Passwords" Value="Help">
<asp:MenuItem Text="Terms & Conditions"
ToolTip="View Terms & Conditions"
Value="Terms" NavigateUrl="http://www.something.com/terms.html"
Target="_parent">
</asp:MenuItem>
<asp:MenuItem Text="Holiday Schedule"
ToolTip="View Holiday Schedule"
Value="Holiday" NavigateUrl="~/Holiday.aspx"></asp:MenuItem>
<asp:MenuItem Text="Shipping Company Information"
ToolTip="View information on shipping companies"
Value="Shipping" NavigateUrl="~/Shipping.aspx"></asp:MenuItem>
<asp:MenuItem NavigateUrl="~/Forgot.aspx" Text="Reset My Password"
ToolTip="Reset your password." Value="Reset">
</asp:MenuItem>
<asp:MenuItem NavigateUrl="~/HowDoI.aspx" Text="How Do I Access my info?"
ToolTip="See how to register, logon, reset your password, change your password and access your account." Value="Retrieve">
</asp:MenuItem>
<asp:MenuItem Text="Register "
ToolTip="Click here to register on this site"
Value="Register2" NavigateUrl="~/Account/Register.aspx"></asp:MenuItem>
</asp:MenuItem>
<asp:MenuItem NavigateUrl="~/Remnant1.aspx" Text="Remnants"
Value="Remnants"></asp:MenuItem>
</Items>
</asp:Menu>
</div>
</div>
<div class="main">
<asp:ContentPlaceHolder ID="MainContent" runat="server"/>
</div>
<div class="clear">
</div>
</div>
<div class="footer">
© 2014 - All Rights Reserved<br />
</div>
</form>
</body>
</html>
Thank you in advance for your help!
I wrote an extension method to find Menu Item by value.
C#
public static class ExtensionMethods
{
public static System.Web.UI.WebControls.MenuItem GetMenuItemByValue(this System.Web.UI.WebControls.Menu menu, string ItemValue)
{
foreach (System.Web.UI.WebControls.MenuItem item in menu.Items)
{
if (item.Value == ItemValue)
{
return item;
}
}
return null;
}
}
VB.NET
Public NotInheritable Class ExtensionMethods
Private Sub New()
End Sub
<System.Runtime.CompilerServices.Extension> _
Public Shared Function GetMenuItemByValue(menu As System.Web.UI.WebControls.Menu, ItemValue As String) As System.Web.UI.WebControls.MenuItem
For Each item As System.Web.UI.WebControls.MenuItem In menu.Items
If item.Value = ItemValue Then
Return item
End If
Next
Return Nothing
End Function
End Class
So then I could do something like..
C#
protected void Page_Load(object sender, EventArgs e)
{
if(!User.IsAuthenticated)
{
NavigationMenu.GetMenuItemByValue("ProdinTran").Visible=false;;
}
}
VB.NET
Protected Sub Page_Load(sender As Object, e As EventArgs)
If Not User.IsAuthenticated Then
NavigationMenu.GetMenuItemByValue("ProdinTran").Visible = False
End If
End Sub
VB.NET placed in Site.master.vb:
Partial Class Site
Inherits System.Web.UI.MasterPage
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not HttpContext.Current.User.Identity.IsAuthenticated Then
NavigationMenu.FindItem("Customer").ChildItems.Remove(NavigationMenu.FindItem("Customer,ProdinTran"))
End If
End Sub
End Class
i am using menu control in asp.net and i am having already 4 menu items, but i want to add 3 more menu items at front.how to achieve this ?
<asp:Menu ID="MainMenu" runat="server" Orientation="Horizontal"
CssClass="styleMenu" Width="100%" >
<Items>
<asp:MenuItem Text="Approval" Value="3"></asp:MenuItem>
<asp:MenuItem Text="Search" Value="4"></asp:MenuItem>
<asp:MenuItem Text="Admin" Value="5"></asp:MenuItem>
<asp:MenuItem Text="Report" Value="6"></asp:MenuItem>
</Items>
</asp:Menu>
I am writing the code for adding dynamic menu items as
MenuItem mnuTest = new MenuItem();
mnuTest.NavigateUrl = "";
mnuTest.Text = "Test";
MainMenu.Items.Add(mnuTest);
but the latest menu is adding at last , how to add at front as 1st menu item
MainMenu.Items.AddAt(0, mnuTest);
I have the following menu control embedded in the Site.master file:
<asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal" RenderingMode="List">
<Items>
<asp:MenuItem NavigateUrl="~/Default.aspx" Text="Home" />
<asp:MenuItem NavigateUrl="~/TechServices.aspx" Text="Tech Services"/>
<asp:MenuItem NavigateUrl="~/HumanResources.aspx" Text="Human Resources"/>
<asp:MenuItem NavigateUrl="~/Marketing.aspx" Text="Marketing"/>
<asp:MenuItem NavigateUrl="~/DocumentControl.aspx" Text="Document Control"/>
<asp:MenuItem NavigateUrl="~/IT.aspx" Text="Information Tech"/>
</Items>
</asp:Menu>
In order to set the CSS class attribute selected I use the following C# code:
protected void Page_Load(object sender, EventArgs e) {
string thispage = this.Page.AppRelativeVirtualPath;
int slashpos = thispage.LastIndexOf('/');
string pagename = thispage.Substring(slashpos + 1);
foreach (MenuItem mi in NavigationMenu.Items) {
if (mi.NavigateUrl.Contains(pagename)) {
mi.Selected = true;
break;
}
}
}
The code above works great. However, these pages now contain sub-pages (children) and I would like to parent pages retain their "Selected" CSS attribute when accessing one of their children pages.
I also created the Web.sitemap file to organize all the parent and their children pages. However, I am stock on how to use the Web.sitemap to help the C# function above to help the parent menu retain their CSS class "selected" attribute. I am not sure if I need the Web.sitemap file for this purpose? The parent and child logic is only available in the Web.sitemap file.
Once you find the MenuItem to select just traverse upward and select all parent. Here's some pseudo-code:
MenuItem miP = mi.Parent;
while (miP != null)
{
miP.Selected = true;
if (miP.Parent == null)
break;
else
miP = miP.Parent;
}
I've been trying to find an easy way of highlighting the current selected menu item of an asp.net menu (so the user knows which page they are on), but no matter what I have tried I can't get it to work. In my markup I have:
<asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal" StaticSelectedStyle-ForeColor="#99CCFF" DynamicSelectedStyle-ForeColor="#99CCFF">
<Items>
<asp:MenuItem NavigateUrl="~/Default.aspx" Text="Operations"/>
<asp:MenuItem NavigateUrl="~/Analysis.aspx" Text="Analysis"/>
<asp:MenuItem NavigateUrl="~/Dashboard.aspx" Text="Dashboard"/>
<asp:MenuItem NavigateUrl="~/Flashboard.aspx" Text="Flashboard"/>
<asp:MenuItem NavigateUrl="~/Spacequest.aspx" Text="SQ OBP"/>
</Items>
</asp:Menu>
And in the server side Page_Load function:
((Menu)Master.FindControl("NavigationMenu")).Items[0].Selected = true;
But this does not work. I tried using a sitemap (even though a sitemap is not what I want to use) and that hasn't worked either. Any ideas?
There's a StaticSelectedStyle property that you can use inside your menu.
<asp:menu [...]>
<staticselectedstyle backcolor="LightBlue"
borderstyle="Solid"
bordercolor="Black"
borderwidth="1"/>
[...]
</asp:menu>
See here for more info.
Also, if there's a class applied to the selected item (which i'm not sure if there is but it would be handy) you can simply hook into that with your CSS. This would be a much nicer way than using the StaticSelectedStyle property.
UPDATE
It's worth noting also that your use of IncludeStyleBlock="false" will stop your menu from generating the CSS necessary to control the selected item.
With the style block turned off, you have to provide your own styles and the auto-generated styles of the menu will not be used.
From MSDN:
If you set this property to false, you cannot set style properties.
For example, you cannot add a DynamicHoverStyle-ForeColor attribute in
markup or set the DynamicHoverStyle.ForeColor property in code.
Source: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.menu.includestyleblock.aspx
I think you'll have to loop through the menu items and see if the current page URL contains the NavigateUrl of the menu item.
foreach (MenuItem item in mn.Items)
{
if (Request.Url.AbsoluteUri.ToLower().Contains(Page.ResolveUrl(item.NavigateUrl.ToLower()))
{
item.Selected = true;
}
}
I would use jQuery in this instance.
For the specified page, so for example on the Analysis.aspx page, add this bit of jquery to your page.
$('#MenuItemID').addClass('active');
Can you specify the ID of the menu items?
Such as:
<asp:MenuItem ID="AnalysisMenuItem" NavigateUrl="~/Analysis.aspx" Text="Analysis"/>
You would then use this:
$('#' + <% AnalysisMenuItem.ClientID %>').addClass('active');
then of course just define what active is in your css:
.active { background-color: #FFF; }
If you are thinking to make it dynamically, then this is the better way:
Menu MyMenu = new Menu();
....
MyMenu.MenuItemDataBound += new MenuEventHandler(MyMenu_MenuItemDataBound);
TheMenu.StaticSelectedStyle.CssClass ="MySelectedClass";
protected void MyMenu_MenuItemDataBound(Object sender, MenuEventArgs e)
{
if (e.Item.NavigateUrl.ToLower().Contains(Path.GetFileName(Request.FilePath).ToLower()))
{
//e.Item.Text = "<div style='color: Yellow'>" + e.Item.Text + " </div>";
e.Item.Selected = true;
}
}
add then simply add .MySelectedClass style to your Css file
..
//Master
<asp:Menu ID="NavigationMenu" runat="server" CssClass="menu"
EnableViewState="False" Orientation="Horizontal"
BackColor="#465C71" DynamicHorizontalOffset="2"
ForeColor="#DDE4EC">
<StaticMenuItemStyle HorizontalPadding="15px" VerticalPadding="2px" />
<StaticSelectedStyle BackColor="#FFFFFF" ForeColor="#000000"/>
<Items>
<asp:MenuItem NavigateUrl="~/Secure/About.aspx" Text="About"/>
<asp:MenuItem NavigateUrl="~/Secure/Login.aspx" Text="Login"/>
</Items>
</asp:Menu>
//Master.cs
foreach (MenuItem item in ((Menu)this.FindControl("NavigationMenu")).Items)
{
if(Request.Url.AbsoluteUri.ToLower().Contains(item.NavigateUrl.ToLower().Substring(1)))
{
item.Selected = true;
}
}
//item.NavigateUrl.ToLower() contains "~". So, find substring and check.
Hi If any of you know how to change a specific Picture on ohver at menuItem in menu control
the code is like this
<asp:Menu ID="NavigationMenu" runat="server" CssClass="menu"
EnableViewState="False" IncludeStyleBlock="False">
<Items>
<asp:MenuItem NavigateUrl="~/Homepage.aspx" ImageUrl="~/images/ONE.png" ></asp:MenuItem>
<asp:MenuItem NavigateUrl="~/AboutUs.aspx" ImageUrl="~/images/SOME.png"></asp:MenuItem>
<asp:MenuItem NavigateUrl="~/ContactUs.aspx" ImageUrl="~/images/MENU.png"></asp:MenuItem>
</Items>
</asp:Menu>
Please show an example, thanks Please ,Please,Please,Please, I need it
You Looking to change this in the page code behind, and when are you trying to do this, on mouse hover or..?
If done in code behind:
NavigationMenu.Items[0].ImageUrl = "whatever.jpg";