Sunday, August 28, 2011

Menu From Database

Menu Database2.html
Default3.aspx
--------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" Debug="true" %>
<!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>Untitled Page</title>
    <style type="text/css">
        .menuItem
{
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
	color: #000000;
	border: 1px solid #C8B678;
	color: #806000;
	background-color: #FFF7DD;
	padding: 4px 5px 4px 5px;
}
 .menuHover
{
	color: #EEEBE6;
	border: 1px Solid #C8B678;
	background-color: #A47E00;
	padding: 4px 5px 4px 5px;
	text-decoration: none;
}
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Menu Width="165px" ID="Menu1" StaticMenuItemStyle-CssClass="menuItem" StaticHoverStyle-CssClass="menuHover"
    DynamicHoverStyle-CssClass="menuHover" DynamicMenuItemStyle-CssClass="menuItem"
    runat="server">
    <StaticItemTemplate>
        <%# Eval("Text") %>
        <asp:Label runat="server" ID="lbItmCount">[<%#Eval("ChildItems.Count")%>]</asp:Label>
    </StaticItemTemplate>
</asp:Menu>
    </div>
    </form>
</body>
</html>
Default3.aspx.cs
------------------
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
partial class Default3 : System.Web.UI.Page
{
    public Default3()
    {
        Load += Page_Load;
    }
    protected void Page_Load(object sender, System.EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindMenu();
        }
    }
    public void BindMenu()
    {
        string connectionString = WebConfigurationManager.ConnectionStrings["DatabaseConnectionString1"].ConnectionString;
        SqlConnection con = new SqlConnection(connectionString);
        SqlDataAdapter dadCategories = new SqlDataAdapter("SELECT CatId,CatName FROM Category order by CatName", con);
        SqlDataAdapter dadSubCat = new SqlDataAdapter("SELECT SubCatId,CatId,SubCatName FROM SubCategory order by SubCatName", con);
        // Add the DataTables to the DataSet
        DataSet dsCat = new DataSet();
        using (con)
        {
            con.Open();
            dadCategories.Fill(dsCat, "Category");
            dadSubCat.Fill(dsCat, "SubCategory");
        }
        // Add a DataRelation
        dsCat.Relations.Add("Children", dsCat.Tables["Category"].Columns["CatId"], dsCat.Tables["SubCategory"].Columns["CatId"]);
        // Add the Category nodes
        int count = 0;
        foreach (DataRow categoryRow in dsCat.Tables["Category"].Rows)
        {
            MenuItem mNode = new MenuItem(Convert.ToString(categoryRow["CatName"]), "", "", "~/DetailView.aspx?CatID=" + Convert.ToString(categoryRow["CatId"]), "_parent");
            Menu1.Items.Add(mNode);
            // Get matching Sub Category
            DataRow[] subCatRows = categoryRow.GetChildRows("Children");
            foreach (DataRow row in subCatRows)
            {
                string subCatName = Convert.ToString(row["SubCatName"]);
                MenuItem subCatItems = new MenuItem(subCatName, "", "", "~/DetailView.aspx?CatID=" + Convert.ToString(row["CatId"]) + "&SubCatID=" + Convert.ToString(row["SubCatId"]), "_parent");
                Menu1.Items[count].ChildItems.Add(subCatItems);
            }
            count = count + 1;
        }
    }
    
}


 



DetailView.aspx
------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DetailView.aspx.cs" Inherits="DetailView" %>
<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        You have selected
        <br />
        Category ID&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :&nbsp;
        <asp:Label ID="lbCatID" runat="server" Text="ID Not Specified!"></asp:Label>
        <br />
        Sub Category ID :&nbsp;
        <asp:Label ID="lbSubCatID" runat="server" Text="ID Not Specified!"></asp:Label>
        <br />
        <br />
        Back to the previous page <a href="Default.aspx">Default.aspx</a></div>
    </form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DetailView.aspx.cs" Inherits="DetailView" %>
<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        You have selected
        <br />
        Category ID&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :&nbsp;
        <asp:Label ID="lbCatID" runat="server" Text="ID Not Specified!"></asp:Label>
        <br />
        Sub Category ID :&nbsp;
        <asp:Label ID="lbSubCatID" runat="server" Text="ID Not Specified!"></asp:Label>
        <br />
        <br />
        Back to the previous page <a href="Default.aspx">Default.aspx</a></div>
    </form>
</body>
</html>
DetailView.aspx.cs
---------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
public partial class DetailView : System.Web.UI.Page
{
    protected void Page_Load(object sender, System.EventArgs e)
    {
        if ((Request.QueryString["CatId"] != null))
        {
            lbCatID.Text = Request.QueryString["CatId"];
        }
        if ((Request.QueryString["SubCatId"] != null))
        {
            lbSubCatID.Text = Request.QueryString["SubCatId"];
        }
    }
    public DetailView()
    {
        Load += Page_Load;
    }
}


output:



Menu Database3_html_m4c6debb1

0 comments:

Post a Comment