tonelm54
asked on
ASP.net add item underneath menu
Im trying to create some code that will add a list of accounts to the menu item 'Accounts' but I want to do it in code, as i want this to be generated in code.
Problem is I keep getting the message:-
Value of type 'System.Web.UI.Control' cannot be converted to 'System.Web.UI.WebControls .MenuItem' .
Does anyone know how I can correct my code to create an item underneath the 'Accounts' menu?
Thanks in advance!!!!
Problem is I keep getting the message:-
Value of type 'System.Web.UI.Control' cannot be converted to 'System.Web.UI.WebControls
Does anyone know how I can correct my code to create an item underneath the 'Accounts' menu?
Thanks in advance!!!!
ASP.net Code:-
---------------------------------------------
<asp:LoginView ID="lvMenu" runat="server">
<LoggedInTemplate>
<asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal">
<Items>
<asp:MenuItem NavigateUrl="~/Default.aspx" Text="Accounts" Value="Accounts">
<asp:MenuItem Text="New Item" Value="New Item"></asp:MenuItem>
</asp:MenuItem>
<asp:MenuItem NavigateUrl="~/transactions.aspx" Text="Transactions"
Value="Transactions"/>
<asp:MenuItem Text="Statments" Value="Statments"></asp:MenuItem>
</Items>
</asp:Menu>
</LoggedInTemplate>
</asp:LoginView>
VB.net Code
---------------------------------------------
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim homeMenuItem As New MenuItem
homeMenuItem = CType(lvMenu.FindControl("NavigationMenu"), MenuItem).Items(0)
Dim newSubMenuItem = New MenuItem("Acc1")
homeMenuItem.ChildItems.Add(newSubMenuItem)
End Sub
It seems to this is fine
Dim homeMenuItem As New Menu
homeMenuItem = CType(lvMenu.FindControl(" Navigation Menu"), Menu)
Dim parentItem As New MenuItem
parentItem = homeMenuItem.FindItem("Acc ounts")
Dim menu1 As New MenuItem
menu1.Text = "Alliance and Leicester"
parentItem.ChildItems.Add( menu1)
-Kusala
Dim homeMenuItem As New Menu
homeMenuItem = CType(lvMenu.FindControl("
Dim parentItem As New MenuItem
parentItem = homeMenuItem.FindItem("Acc
Dim menu1 As New MenuItem
menu1.Text = "Alliance and Leicester"
parentItem.ChildItems.Add(
-Kusala
ASKER
Hmm, still doesnt work!!!
Think I may have found the issue, but again no idea how to fix it!
The asp:menu is actually on the site.master page, and the code is in site.master.vb but it cannot find the menu control in the site.master!
Any ideas?
Think I may have found the issue, but again no idea how to fix it!
The asp:menu is actually on the site.master page, and the code is in site.master.vb but it cannot find the menu control in the site.master!
Any ideas?
ASKER
So my master page code is currently:-
<%@ Master Language="VB" AutoEventWireup="false" CodeBehind="Site.master.vb" Inherits="OAM.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>
OAM - Online account management
</h1>
</div>
<div class="loginDisplay">
<asp:LoginView ID="HeadLoginView" runat="server" EnableViewState="false">
<AnonymousTemplate>
[ <a href="~/Account/Login.aspx" ID="HeadLoginStatus" runat="server">Log In</a> ]
</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" id="divMenu" runat="server">
<asp:LoginView ID="lvMenu" runat="server">
<LoggedInTemplate>
<asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal">
<Items>
<asp:MenuItem NavigateUrl="~/Default.aspx" Text="Accounts" Value="Accounts">
<asp:MenuItem Text="New Item" Value="New Item"></asp:MenuItem>
</asp:MenuItem>
<asp:MenuItem NavigateUrl="~/transactions.aspx" Text="Transactions"
Value="Transactions"/>
<asp:MenuItem Text="Statments" Value="Statments"></asp:MenuItem>
</Items>
</asp:Menu>
</LoggedInTemplate>
</asp:LoginView>
</div>
</div>
<div class="main">
<asp:ContentPlaceHolder ID="MainContent" runat="server"/>
</div>
<div class="clear">
</div>
</div>
<div class="footer">
</div>
</form>
</body>
</html>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Which seems to point to using the code such as:-
Dim homeMenuItem As New Menu
homeMenuItem = CType(lvMenu.FindControl("
Dim parentItem As New MenuItem
parentItem = homeMenuItem.FindItem("Acc
Dim menu1 As New MenuItem
menu1.Text = "Alliance and Leicester"
parentItem.ChildItems.Add(
But still get the error message:-
"Object reference not set to an instance of an object." on the line "parentItem = homeMenuItem.FindItem("Acc