Wednesday, July 28, 2010

LeftMenu UserControl using ListView in ASP.Net

 This is a leftmenu usercontrol that fetch datas (Menu Items and submenu items) from database and build a  userinteface with good look and feel.


 From above leftmenu, we have two categories(Mobile and Laptop) in database  and subcategories are listed under the corresponding category. By using this user control in our master page, it is very easy to control navigation menu. We will do any functionalities or redirect to any aspx pages on click event of each subacategories in the list.

Here I am posted the complete code.

UC_LeftMenu.ascx
----------------------

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UC_LeftMenu.ascx.cs"
    Inherits="eMall.UserControls.UC_LeftMenu" %>
<link href="../styles/w4d.css" rel="stylesheet" type="text/css" />
<div id="boxbg">
    <asp:ListView runat="server" ID="RightMenuItems" ItemPlaceholderID="PlaceHolder2">
        <LayoutTemplate>
            <asp:PlaceHolder runat="server" ID="PlaceHolder2" />
        </LayoutTemplate>
        <ItemTemplate>
            <asp:LinkButton ID="lnkBtnTest" runat="server" CommandName="testCommand" />
            <h1>
                <%# Eval("Name") %></h1>
            <asp:ListView runat="server" ID="subMenu" ItemPlaceholderID="PlaceHolder3" DataSource='<%# Eval("subCategories") %>'
                OnItemCommand="listViewTest_ItemCommand">
                <LayoutTemplate>
                    <table width="100%" border="0" cellpadding="0" cellspacing="0" class="list">
                        <tr>
                            <td>
                                <asp:PlaceHolder runat="server" ID="PlaceHolder3" />
                            </td>
                        </tr>
                    </table>
                </LayoutTemplate>
                <ItemTemplate>
                    <asp:LinkButton ID="lnkBtnSubMenu" runat="server" Text='<%# Eval("Name") %>'
                    CommandName="clickSubMenu"  CommandArgument='<%# Eval("ID") %>' />
                    <asp:Label runat="server" ID="lblID" Visible="false" Text='<%# Eval("ID") %>' />
                </ItemTemplate>
            </asp:ListView>
        </ItemTemplate>
    </asp:ListView>
</div>

UC_LeftMenu.ascx.cs
-------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BusinussLayer;
using Entity;

namespace eMall.UserControls
{
    public partial class UC_LeftMenu : System.Web.UI.UserControl
    {        
        protected void Page_Load(object sender, EventArgs e)
        {
            fillMenus();
            if (!IsPostBack)
            {
                //fillMenus();
            }
        }

        private void fillMenus()
        {
            DataTable dtTblCategory = new DataTable();
            dtTblCategory = (new eMallBL()).getCategories(0);
            DataTable dtTblSubCategory = new DataTable();
            dtTblSubCategory = (new eMallBL()).getSubCategories(0, 0);

            IList<eMallEntity.ItemCatagory> categories = new List<eMallEntity.ItemCatagory>();
            for (int i = 0; i < dtTblCategory.Rows.Count; i++)
            {
                eMallEntity.ItemCatagory category = new eMallEntity.ItemCatagory();
                category.Name = dtTblCategory.Rows[i]["Name"].ToString();
                category.ID = Convert.ToInt32(dtTblCategory.Rows[i]["ID"].ToString());
                IList<eMallEntity.ItemSubCatagory> subCategories = new List<eMallEntity.ItemSubCatagory>();
                for (int j = 0; j < dtTblSubCategory.Rows.Count; j++)
                {
                    if (dtTblSubCategory.Rows[j]["CategoryID"].ToString() == dtTblCategory.Rows[i]["ID"].ToString())
                    {
                        eMallEntity.ItemSubCatagory subCategory = new eMallEntity.ItemSubCatagory();
                        subCategory.Name = dtTblSubCategory.Rows[j]["Name"].ToString();
                        subCategory.ID = Convert.ToInt32(dtTblSubCategory.Rows[j]["ID"].ToString());
                        subCategories.Add(subCategory);
                    }
                }
                category.subCategories = subCategories;
                categories.Add(category);
            }
            RightMenuItems.DataSource = categories;
            RightMenuItems.DataBind();

            //listViewTest.DataSource = dtTblCategory;
            //listViewTest.DataBind();
        }

        protected void listViewTest_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "clickSubMenu":
                    Label lblSubCategoryID = (Label)e.Item.FindControl("lblID");
                    Context.Items.Add("subCategoryID", lblSubCategoryID.Text);
                    Server.Transfer("AddItems.aspx");
                    break;
            }
        }
    }
}



No comments:

Post a Comment