ASP.NET - show menu dynamically after login

Asked By Nitish Gupta
03-Sep-11 11:23 AM
hello,
i want to show some menu dynamically after user login ..but problem is that i have made master page in which i am showing some menu..but when user login ..i want to show some other menu also to user like Profile,Lo-gout etc..in all pages..kindly help me out..is there is any other good approach to implement above feature ..please tell me that also..
please tell me full code and source view also..
i am using asp.net C# and sql server 2008..
thanks in advance..
  Devil Scorpio replied to Nitish Gupta
03-Sep-11 11:27 AM
Hi nitish,

U can show menu dynamically after login


u can do some thing like this on Page_Load:
protected void Page_Load(object sender, EventArgs e)
   
{
       
if (Session["IsLoggedIn"] == "1")
       
{
            Create_Menu1
();
       
}
       
else
       
{
            Create_Menu
();
       
}
   
}
  Vickey F replied to Nitish Gupta
03-Sep-11 11:32 AM
for that design your menu dynamically.

Dynamic Menu control is very useful in asp.net these are menu which you can see on every site, by menu control you can navigate from one page to another page.


So follow these steps to create Dynamic Menu Control The Steps are:


1- Start -> All Programs -> Visual Studio 2005 or Visual Studio 2008

2- Now go to File Menu -> New -> Web Site

3- Under Visual Studio Installed Template-> Choose ASP.NET WEB SITE -> Choose File System from the location combo box -> Set the path by the browse button - > Choose the language from the Language ComboBox (Visual C# , Visual Basic , J #)
Choose Visual C#

4 - Click on the OK Button:-

First you have to learn XML and HTML to work with ASP .NET_
I have used <! - -> to make the text as comment this is the way to make comment in this code .

This is the Source Code window and in this page you will se this code.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Type your webpage title here</title> <!- Here you can specify your page title in between title tag ->
</head>
<body>
<form id="form1? runat="server">
<div>
<!- In between div tags you can manage your web controls like buttons, labels, picture
Box, ImageMap etc ->
</div>
</form>
</body>
</html>

See here is a tab named Design in the bottom of this page

5- Click on this tab and you will see a blank web page where you can drag any control from the toolbox (which is in the left side of this window)
Open Web.config file and write true in place of false, See
<compilation debug="true">

6- Drag a Menu Control from the navigation control tab


7-Now go in solution explorer and right click on the website and choose Add new item, select XML file
now again choose Add new item, select site Map .
8- Now open XML file and type this code in this.

<?xml version="1.0" encoding="utf-8" ?>
<application>
<setction title="Section 1" value="default.aspx">
<subpage title ="Page 1" value="page1.aspx"/>
<subpage title ="Page 2" value="page2.aspx"/>
<subpage title ="Page 3" value="page3.aspx"/>
</setction>
<setction title="Section 2" value="default.aspx">
<subpage title ="Page 1" value="page1.aspx"/>
<subpage title ="Page 2" value="page2.aspx"/>
<subpage title ="Page 3" value="page3.aspx"/>
</setction>
</application>

9- Now open your site Map file and type this code in it.

<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
<siteMapNode url= "Default.aspx" title= "Section 1" description="">
<siteMapNode url="Page1.aspx" title="Page 1" description="" />
<siteMapNode url="Page2.aspx" title="Page 2" description="" />
<siteMapNode url="Page3.aspx" title="Page 3" description="" />
</siteMapNode>
</siteMap>

10- In Design mode select menu control and click on the arrow (seems when you move cursor on menu control)
Choose Data Source -> choose XML or Site Map Data Source
Ok

11 - Now run your web site by Ctrl + F5

Now you will get output.
  Radhika roy replied to Nitish Gupta
03-Sep-11 11:35 AM
Follow this example , here i am creating menu using xml file.

xml file-

<?xml version="1.0" encoding="utf-8" ?>
<siteMap>
 <siteMapNode url="Default.aspx" title="Home">
   <siteMapNode title="Products" description="Our Products">
    <siteMapNode url="Product1.aspx" title="My Products" 
      description="These are my products" />
    <siteMapNode url="Product2.aspx" title="New Products" 
      description="Some new products " />
   </siteMapNode>
   <siteMapNode title="Services" description="Our Services">
    <siteMapNode url="Service1.aspx" title="http://asp.net/ Consulting"
      description="Best http://asp.net/ Consulting" />
    <siteMapNode url="Service2.aspx" title="http://asp.net/ Training" 
      description="Best http://asp.net/ Training" />
   </siteMapNode>
 </siteMapNode>
</siteMap>

Now use this code to design menu using xml.


private void CreateMenuWithXmlFile()
{
  string path = @"C:\MyXmlFile.xml";
  DataSet ds = new DataSet();
  ds.ReadXml(path);
  Menu menu = new Menu();
  menu.MenuItemClick += new MenuEventHandler(menu_MenuItemClick);

  for (int i = 0; i < ds.Tables.Count; i++)
  {
    MenuItem parentItem = new MenuItem((string)ds.Tables[i].TableName);
    menu.Items.Add(parentItem);

    for (int c = 0; c < ds.Tables[i].Columns.Count; c++)
    {
    MenuItem column = new MenuItem((string)ds.Tables[i].Columns[c].ColumnName);
    menu.Items.Add(column);

    for (int r = 0; r < ds.Tables[i].Rows.Count; r++)
    {
      MenuItem row = new MenuItem((string)ds.Tables[i].Rows[r][c].ToString());
      parentItem.ChildItems.Add(row);
    }
    }
  }

  Panel1.Controls.Add(menu);
  Panel1.DataBind();
}

follow this link also-

http://www.codeproject.com/KB/menus/PopulatingMenuControlASP2.aspx

Hope this will help you.
  Riley K replied to Nitish Gupta
03-Sep-11 11:37 AM

You can remove unwanted menu items in Page_Load, like this:

protected void Page_Load(object sender, EventArgs e)
{
  if (!Roles.IsUserInRole("Admin"))
  {
    MenuItemCollection menuItems = mTopMenu.Items;
    MenuItem adminItem = new MenuItem();
    foreach (MenuItem menuItem in menuItems)
    {
      if (menuItem.Text == "Roles")
        adminItem = menuItem;
    }
    menuItems.Remove(adminItem);
  }
}

I'm sure there's a neater way to find the right item to remove, but this one works. You could also add all the wanted menu items in a Page_Load method, instead of adding them in the markup.

Regards

  Vickey F replied to Nitish Gupta
03-Sep-11 11:39 AM
for that design your menu dynamically.

Dynamic Menu control is very useful in asp.net these are menu which you can see on every site, by menu control you can navigate from one page to another page.


So follow these steps to create Dynamic Menu Control The Steps are:


1- Start -> All Programs -> Visual Studio 2005 or Visual Studio 2008

2- Now go to File Menu -> New -> Web Site

3- Under Visual Studio Installed Template-> Choose ASP.NET WEB SITE -> Choose File System from the location combo box -> Set the path by the browse button - > Choose the language from the Language ComboBox (Visual C# , Visual Basic , J #)
Choose Visual C#

4 - Click on the OK Button:-

First you have to learn XML and HTML to work with ASP .NET_
I have used <! - -> to make the text as comment this is the way to make comment in this code .

This is the Source Code window and in this page you will se this code.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Type your webpage title here</title> <!- Here you can specify your page title in between title tag ->
</head>
<body>
<form id="form1? runat="server">
<div>
<!- In between div tags you can manage your web controls like buttons, labels, picture
Box, ImageMap etc ->
</div>
</form>
</body>
</html>

See here is a tab named Design in the bottom of this page

5- Click on this tab and you will see a blank web page where you can drag any control from the toolbox (which is in the left side of this window)
Open Web.config file and write true in place of false, See
<compilation debug="true">

6- Drag a Menu Control from the navigation control tab


7-Now go in solution explorer and right click on the website and choose Add new item, select XML file
now again choose Add new item, select site Map .
8- Now open XML file and type this code in this.

<?xml version="1.0" encoding="utf-8" ?>
<application>
<setction title="Section 1" value="default.aspx">
<subpage title ="Page 1" value="page1.aspx"/>
<subpage title ="Page 2" value="page2.aspx"/>
<subpage title ="Page 3" value="page3.aspx"/>
</setction>
<setction title="Section 2" value="default.aspx">
<subpage title ="Page 1" value="page1.aspx"/>
<subpage title ="Page 2" value="page2.aspx"/>
<subpage title ="Page 3" value="page3.aspx"/>
</setction>
</application>

9- Now open your site Map file and type this code in it.

<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
<siteMapNode url= "Default.aspx" title= "Section 1" description="">
<siteMapNode url="Page1.aspx" title="Page 1" description="" />
<siteMapNode url="Page2.aspx" title="Page 2" description="" />
<siteMapNode url="Page3.aspx" title="Page 3" description="" />
</siteMapNode>
</siteMap>

10- In Design mode select menu control and click on the arrow (seems when you move cursor on menu control)
Choose Data Source -> choose XML or Site Map Data Source
Ok

11 - Now run your web site by Ctrl + F5

Now you will get output.
  dipa ahuja replied to Nitish Gupta
03-Sep-11 01:12 PM
Two options you have for that:

Choose the readymad login control available in the Login tab and use the LoginView Control

<asp:Login ID="Login2" runat="server"
CreateUserUrl="newuser.aspx"
DestinationPageUrl="home.aspx"    
VisibleWhenLoggedIn="False" 
</asp:Login>   

<asp:LoginView ID="LoginView3" Runat="server">
  <LoggedInTemplate>
  <asp:LinkButton ID="LinkButton2" runat="server" Text="My Profike" 
  PostBackUrl="Modifyacc.aspx" /><br />
    <asp:LinkButton ID="LinkButton3" runat="server" Text="Logout" 
    PostBackUrl="Logout.aspx" /><br />    
  <asp:LoginStatus LoginText="Welcome Guest!!" id="LoginStatus" runat="server" />
  </LoggedInTemplate>
  <AnonymousTemplate>
  </AnonymousTemplate>
</asp:LoginView>
Hope this will help you!

  Rohan Dave replied to Nitish Gupta
03-Sep-11 01:26 PM
you can simply do it by implemeting some user rights mechanism in your application. Based on the user rights you can show specific menu assigned to particular user..

Like i have table having following table structure for storing menu information..
ParentChildMenuID Int Identity PK   // primary key of table
keyName varchar(50)   // this is just for using in coding side.
MenuName varchar(50)    // this the original menu name
ParentMenuID int    // store menu value
IsParent bit    // if 1 then parent else child...

Other table holds user right for that particular menu.. the table structure of this table looks like below ...

UserRightID bigint identity   PK   // primary key of table
UserLoginID bigint  FK // foreign key of Usermaster table
ParentMenuId int FK    // foreign key of menu table
OnlyVisible bit     // if 1 it means show that menu to user otherwise don't show..

now based on the above table structure you can write a code in asp.net and achieve your requirement..

Create New Account
help
help. I would greatly appreciate it. Thanks, George - -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - see below for IIS6.log - -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - [3 / 5 / 2005 12:57:45] LogFile Open. [* ** ** Search on FAIL / MessageBox keywords for failures * ** **]. [3 / 5 / 2005 12:57:45] Initial thread locale = 409 [3 / 5 / 2005 12:57:45] returned from France fix with locale 409 [3 / 5 2005 12:57:45] OC_PREINITIALIZE:[iis] End. Return = 1 (OCFLAG_UNICODE) [3 / 5 / 2005 12:57:45] OC_INIT_COMPONENT:[iis, (null)] Start. [3 / 5 / 2005 12:57:45] OC_INIT_COMPONENT:3 / 31 / 2003 12:00:00 A_ __ __ __ 6.0.2600.2180 0.2600.2180 (xpsp_sp2_rtm.040803-2158): x86: C: \ WINDOWS \ system32 \ Setup \ iis.dll [3 / 5 / 2005 12:57:45] OC_INIT_COMPONENT:Set UnAttendFlag:OFF (File = '') [3 / 5 / 2005 12:57:45] OC_INIT_COMPONENT:CmdLine = "C: \ WINDOWS \ system32 \ sysocmgr.exe" / y / i:C: \ WINDOWS \ system32
visual studio installation problem Actually, my OS is Windows Xp with service pack2.I added service pack3 to install visual studio2010.after that i tryed to installed, but am getting SETUP FAILED due to "Windows XP is not installed. [08 / 10 / 11, 14:26:00] VS70pgui: [2] DepCheck indicates Microsoft Visual F# 2.0 Runtime was not attempted to be installed. [08 / 10 / 11, 14:26:00] VS70pgui: [2] DepCheck indicates Microsoft Visual Studio Macro Tools was not attempted to be installed. [08 / 10 / 11, 14:26:00] VS70pgui attempted to be installed. [08 / 10 / 11, 14:26:01] VS70pgui: [2] DepCheck indicates Microsoft Visual Studio 2010 Professional - ENU was not attempted to be installed. [08 / 10 / 11, 14:26:01
alot var Hi What are Master Pages in ASP.NET? or What is a Master Page? ASP.NET master pages allow you to create a consistent layout for the pages in your application. A single master page defines the look and feel and standard behavior that you want for all of the you want to display. When users request the content pages, they merge with the master page to produce output that combines the layout of the master page with the content from the content page. What are the 2 important parts of a master page? The following are the 2 important parts of a master page 1. The Master Page itself 2. One or more Content Pages Can Master Pages be nested? Yes, Master Pages
Microsoft.Jet.OLEDB.4.0;”+ _ “Data Source = C: \ Documents and Settings \ User \ My Documents \ Visual Studio Projects \ 1209 \ db1.mdb”+ _ “User ID = Admin;”+ _ “Password = ;”); Dim cmd As New OleDbCommand while creating an ASP.NET application? There are two level of asp.net debugging 1. Page level debugging For this we have to edit the page level debugging enable the trace to true in the line in the html format of the page. %@ Page Language = ”vb” trace = ”true”AutoEventWireup = ”false” Codebehind = ”WebForm1.aspx.vb” Inherits = ”WebApplication2.WebForm1?&gt; 2 Enable trace enabled = true. If there is a calendar control to be included in each page of your application, and and we do not intend to use the Microsoft-provided calendar do you develop it? Do you copy and paste the code into each and every page of your application? Create the Calendar User Control The control we will create will contain