Friday, March 30, 2012

Dynamic menu ASP.NET Working on All Browser(Safari, Chrome, FireFox)


Hello Friends,



Dynamic menu is needed where Admin wishes to distribute work and at the same time he wants each people should do the work that has been assign to him.

For example Admin is a shop owner and he has employee like 
1: sales man, 
2: storekeeper,
3: dataentry operator

sales man should not interfere in storekeeper's or Dataentry operator's work,

Similarly   storekeeper should not interfere in  sales man's or Dataentry operator's work,

BUT Dataentry operator CAN interfere with both storekeeper's  and   sales man's  work

One single menu will not work here and authorization can be changed time to time thus we need a dynamic menu.

Step 1: creat login table as given example below









Create a Mastermenu table with all menus as shown below



















Create Table Dynamic As Shown below.
CREATE TABLE [dbo].[Dynamic](
[MenuID] [int] NULL,
[Text] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Description] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[ParentID] [int] NULL,
[Fkadminid] [int] NULL
) ON [PRIMARY]


LOGIN.ASPX

CREATE  a login page and if admin is logging in then go to Access Specify.aspx To provide different Menu Access  to different users  
Else if other user is logging in then
go to UserHome.aspx with Admin Decided menu
------------------------------------------------------------------------------------------------------------

Dynamic Menu

<%@ 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 id="Head1" runat="server">
    <title>DataBase Driven Menu</title>
    <meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Expires" content="0">

    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<style type="text/css" >
.IE8Fix 
    z-index: 1000; 

}
</style>

</head>

<body>
    <form id="form1" runat="server">
    <div style="width:1269px;">
        <asp:LinkButton ID="LinkButton1" runat="server" style="float:right;" 
            onclick="LinkButton1_Click">LOG OUT</asp:LinkButton></div>
    <div>
       <asp:Menu ID="Menu1" DataSourceID="xmlDataSource" runat="server" 
          BackColor="#FFFBD6" DynamicHorizontalOffset="2" Font-Names="Verdana" 
          ForeColor="#990000" StaticSubMenuIndent="10px" StaticDisplayLevels="1" Orientation="Horizontal" >
          <DataBindings>
            <asp:MenuItemBinding DataMember="MenuItem" 
              NavigateUrlField="NavigateUrl" TextField="Text" ToolTipField="ToolTip"/>
             
          </DataBindings>
          <StaticSelectedStyle BackColor="#FFCC66" />
          <StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
          <DynamicMenuStyle BackColor="#FFFBD6" CssClass="IE8Fix" />
          <DynamicSelectedStyle BackColor="#FFCC66" />
          <DynamicMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
          <DynamicHoverStyle BackColor="#990000" Font-Bold="False" ForeColor="White"/>
          <StaticHoverStyle BackColor="#990000" Font-Bold="False" ForeColor="White" />
       </asp:Menu>
       <asp:XmlDataSource ID="xmlDataSource" TransformFile="~/TransformXSLT.xsl"  EnableCaching="false"  
          XPath="MenuItems/MenuItem" runat="server"/> 
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:AcharyaConnectionString %>" 
            SelectCommand="SELECT [MenuID], [Text] FROM [DynaMenu]"></asp:SqlDataSource>
        <br />
        <br />
    </div>
    </form>
</body>
</html>

Dynamicmenu.aspx.cs

public partial class _Default : System.Web.UI.Page 
{
    DataSet ds;
    SqlConnection conn;
    string connStr;
    string sql;
    SqlDataAdapter da;
    protected override void OnPreInit(EventArgs e)
    {

        if (Request.UserAgent != null &&

            (Request.UserAgent.IndexOf("AppleWebKit") > 0))
        {

            this.ClientTarget = "uplevel";

        }

        base.OnPreInit(e);

    }


    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            
            int sss = int.Parse( Session["userid"].ToString());
            string sid = Session["userid"].ToString();
            //Response.Write(sid);

            ds = new DataSet();
            connStr = "Data Source=ITPL-16;Initial Catalog=Acharya;Integrated Security=True";
            using (conn = new SqlConnection(connStr))
            {
                sql = "Select MenuID, Text, Description, ParentID from Dynamic where  fkadminid='"+sid+"'";
                da = new SqlDataAdapter(sql, conn);
                da.Fill(ds);
                da.Dispose();
                conn.Close();
                conn.Dispose();
            }

            ds.DataSetName = "Menus";
            ds.Tables[0].TableName = "Menu";
            DataRelation relation = new DataRelation("ParentChild",
                    ds.Tables["Menu"].Columns["MenuID"],
                    ds.Tables["Menu"].Columns["ParentID"],
                    true);

            relation.Nested = true;
            ds.Relations.Add(relation);

            xmlDataSource.Data = ds.GetXml();
            
            ds.Clear();
            if (Request.Params["Sel"] != null)
            {
                string ss = Request.Params["Sel"];
                Response.Redirect(Request.Params["Sel"]);
                Page.Controls.Add(new System.Web.UI.LiteralControl("You selected " + Request.Params["Sel"]));
            }
            Menu1.DataBind();
        }
        //Response.Cache.SetCacheability(HttpCacheability.NoCache);
        
       

       
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        
        Session.Clear();
        Session.RemoveAll();
        Session.Abandon();
        Session.Remove("userid");           
        da = new SqlDataAdapter();
        da.Dispose();       
        xmlDataSource.Data = null;
        Response.Redirect("Login.aspx");
        
    }
}

Create a TransformXSLT.xsl   file as shown below

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" encoding="utf-8"/>
  <!-- Find the root node called Menus 
       and call MenuListing for its children -->
  <xsl:template match="/Menus">
    <MenuItems>
      <xsl:call-template name="MenuListing" />
    </MenuItems>
  </xsl:template>
  
  <!-- Allow for recusive child node processing -->
  <xsl:template name="MenuListing">
    <xsl:apply-templates select="Menu" />
  </xsl:template>
  
  <xsl:template match="Menu">
    <MenuItem>
      <!-- Convert Menu child elements to MenuItem attributes -->
      <xsl:attribute name="Text">
        <xsl:value-of select="Text"/>
      </xsl:attribute>
      <xsl:attribute name="ToolTip">
        <xsl:value-of select="Text"/>
        
      </xsl:attribute>
      <xsl:attribute name="NavigateUrl"> 
        
        <xsl:text>?Sel=</xsl:text>
        <xsl:value-of select="Description"/>
      </xsl:attribute>
      
      <!-- Call MenuListing if there are child Menu nodes -->
      <xsl:if test="count(Menu) > 0">
        <xsl:call-template name="MenuListing" />
      </xsl:if>
    </MenuItem>
  </xsl:template>
</xsl:stylesheet>

--------------------------------------------N Joy------------------------------------------------------








No comments:

Post a Comment