Thursday, March 26, 2009

Sample creating Menu at master file using sql-server Table and keeping menu items at database using asp,net,C# , asp 2.0

Sample creating Menu at master file using sql-server Table and keeping menu items at database using asp,net,C# , asp 2.0 & menu control.

Table 

CREATE TABLE [dbo].[ovMenu] (
[MenuID] [int] IDENTITY (1, 1) NOT NULL ,
[Text] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[Description] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[ParentID] [int] NULL 
) ON [PRIMARY]
GO
sample data
MenuID Text Description ParentID
22 Overrides Commission
23 Reports Reports
31 Admin
32 Support Support
33 ABC 22
34 XYZ XYZ 22
35 ABC 23
36 XYZ XYZ 23
37 Detail Report overrep2 34
38 Summary Report ovsumrept 34
39 Adv Prc Error advprcerr 36
40 SRP Error srperrrep 36
41 Commission ovowncomrep 36
42 Enter Override ownerdetals 34
43 Update Override overupdate 34
44 Commission Default 35
46 Detail Report innoverrep1 33
47 Summary Report ovsuminnrep 33
48 Monthly Report ovsminnmth 33
49 Monthly Report ovsmsumrep 34
50 Source Report oversortblr 33
55 ABC 31
56 XYZ 31
57 Data Update 55
58 Full Update 55
59 Data Update 56
60 Unit Cost Error scUncstWTerr 36
61 Qtm. Override quantumov 33
62 ETC-EB etceb 36


XML

TransformXSLT.xsl

<?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="Description"/>
      </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>



Html Code

<%@ Master  Language="C#"  AutoEventWireup="true" CodeFile="MasterReport.master.cs" Inherits="MasterReport" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<head runat="server">
    <title>Untitled Page</title>



</head>
<body style="margin-top:2%; margin-left:2%; margin-right:2%; margin-bottom:2%; background-color:#6699ff; height:100%";  >
    <form id="form1" runat="server">
    <div>
        <table border="0" cellpadding="0" cellspacing="0"  style="width:100%; height:100%" id="TABLE1" onclick="return TABLE1_onclick()" >
            <tr>
                <td bgcolor="#000099" bordercolordark="#ffffff" colspan="2" style="height: 1%" valign="top"
                    width="100%">&nbsp;
                    </td>
            </tr>
            <tr>
                <td colspan="2" style="height: 1%;"   valign="top" bgcolor="#0033cc" width="100%" bordercolordark="#ffffff">
                    <font  color="white"> </font><span style="font-size: 20px; color: white">&nbsp;&nbsp;<br />
                        &nbsp; &nbsp;<strong>Sales Override &amp; Commission</strong></span><br />
                        
                        
                    &nbsp;</td>
            </tr>
            <tr>
                <td bgcolor="#000099" colspan="2" bordercolor="Red" style="height: 1%" valign="top" width="100%" bordercolordark="#ffffff" align="right">
                    <font color="#ffffff"><b>&nbsp;&nbsp;&nbsp;Welcome</b></font>
                    
                    <asp:Label ID="Label1"   runat="server" Text="Label" Width="57px" Font-Bold="True" ForeColor="White" BackColor="Transparent"></asp:Label>
                    
                   &nbsp; <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Logout" Height="20px" Width="52px"  />&nbsp;</td>
            </tr>
            <tr>
                <td  valign="top"  bgcolor="Lavender" width="20%">
                <table  cellspacing="0" cellpadding="15" border="0" width="99%" height="1%"  >
        <tr>
          
          <td valign="top" >
            <asp:contentplaceholder id="MainBody" runat="server"  >
                <asp:Menu ID="Menu1" runat="server" BackColor="Transparent" DataSourceID="xmlDataSource"
                    DynamicHorizontalOffset="1" Font-Names="arial" ForeColor="#990000" StaticDisplayLevels="1"
                    StaticSubMenuIndent="10px" Width="99%" BorderWidth="1" BorderColor="AliceBlue">
                    <DataBindings>
                     <asp:MenuItemBinding DataMember="MenuItem" NavigateUrlField="NavigateUrl" TextField="Text" 
                     ToolTipField="ToolTip"  />
                    </DataBindings>
                    <StaticSelectedStyle BackColor="Red" Font-Bold="true" BorderStyle="Solid" />
                    <DynamicMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" BorderWidth="1px" BorderColor="AliceBlue" BorderStyle="Solid" Font-Bold="true"/>
                    <DynamicHoverStyle  BackColor="#ffffcc"  ForeColor="Black" BorderWidth="1px"  Font-Bold="true" BorderColor="AliceBlue" BorderStyle="Solid" />
                    <StaticHoverStyle BackColor="#ffffcc"  Font-Bold="True" ForeColor="Black" BorderStyle="Solid" />
                    <StaticMenuItemStyle BorderWidth="1px" BorderColor="AliceBlue" BorderStyle="Solid" HorizontalPadding="5px" VerticalPadding="2px" BackColor="#d8e4f8" Font-Bold="true" ForeColor="Blue"/>
                    <DynamicMenuStyle BorderWidth="1px"    BorderColor="AliceBlue" BorderStyle="Solid" HorizontalPadding="5px" VerticalPadding="2px" BackColor="#d8e4f8" Font-Bold="true" ForeColor="Blue"/>
                </asp:Menu>
                
            
      &nbsp;<asp:XmlDataSource ID="xmlDataSource" TransformFile="~/TransformXSLT.xsl" XPath="MenuItems/MenuItem" runat="server"/> 
          </asp:contentplaceholder>
            <div>
    </div>
            
          </td>
        </tr>
        
      </table>
      
                </td>
                <td  valign="top" align="left" bgcolor="white" width="100%" >
                <asp:contentplaceholder id="mainContent" runat="server">
                    
                </asp:contentplaceholder>
                </td>
            </tr>
            <tr>
                <td colspan="2" bgcolor="#0033cc" align="center" > <font color="#ffffff">©2007 xxxxxx LLC</font>
                </td>
            </tr>
            
        </table>
        
    </div>
    </form>
</body>
</html>





code behind

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.OleDb;

public partial class MasterReport : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        //Request.UserAgent.IndexOf("IE") != -1).

        if (Session["Usrid"]==null) 
        {
            Response.Redirect("Login.aspx");
        }


        Label1.Text = Session["Usrid"].ToString();
        Label1.ForeColor = System.Drawing.Color.White;

        {

            DataSet ds = new DataSet();
            string connStr = "server=SERVER;Initial Catalog=DB;User ID=DB;Password=DB";
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                string sql = "Select MenuID, Text, Description, ParentID from ovMenu";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                da.Fill(ds);
                da.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();

           // Response.Write(ds.GetXml());


            if (Request.Params["Sel"] != null)
                if (Request.Params["Sel"] == "Smart Circle")
                {
                    Response.Redirect("overrep2.aspx");
                }
                else
                   Response.Redirect(Request.Params["Sel"]+".aspx");
               

                
             //Page.Controls.Add(new System.Web.UI.LiteralControl("You selected " + Request.Params["Sel"]));
        }
    }




    protected void Button1_Click(object sender, EventArgs e)
    {
  FormsAuthentication.SignOut();
  FormsAuthentication.RedirectToLoginPage();

    }
}

No comments: