Skip to main content

Dynamic Menu in asp.net using Listview

 1.

   public DataTable LoadMenu()

        {

            MenuBal MenuBal = new MenuBal();

            DataSet ds = this.GetData(0);

            PopulateMenu(ds);

            return ds.Tables[2];

        }


    private DataSet GetData(int parentMenuId)

        {

            MenuBal objmenu = new MenuBal();

            string Mode = "";

            if (Session["Mode"] != null)

            {

                Mode = Session["Mode"].ToString();

            }

            DataSet ds = objmenu.GetData(parentMenuId, Convert.ToInt32(SessionHelper.CurrentUserId), Mode, SessionHelper.CurrentOrganizationType);

            return ds;

        }




  public DataSet GetData(int parentMenuId, int userId,string Module,string OrganizationType)

        {

            MenuMasterDal Menu = new MenuMasterDal();

            return Menu.GetData(parentMenuId, userId, Module, OrganizationType);

        }



            try

            {

                DataAccessObject DataObject = new DataAccessObject();

                return DataObject.RetrieveDataSet(CHAConstants.CHAConnectionString, "PCS_Get_MenuMaster",

                    new StoreProcedureParameter { ParameterName = "@ParentMenuID", DbType = DataTypeConstants.INT, IsInParameter = true, Value = ParentMenuID },

                    new StoreProcedureParameter { ParameterName = "@userId", DbType = DataTypeConstants.INT, IsInParameter = true, Value = userId },

                    new StoreProcedureParameter { ParameterName = "@Module", DbType = DataTypeConstants.VARCHAR, IsInParameter = true, Value = Module },

                    new StoreProcedureParameter { ParameterName = "@OrganizationType", DbType = DataTypeConstants.VARCHAR, IsInParameter = true, Value = OrganizationType });

            }



2. Procedure



USE [PCS_CODEX]

GO

/****** Object:  StoredProcedure [dbo].[PCS_Get_MenuMaster]    Script Date: 06-Oct-20 3:17:06 PM ******/

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO


--exec [PCS_Get_MenuMaster] 0, 'Import' , 197 , 'Shipping Line'

ALTER Proc [dbo].[PCS_Get_MenuMaster] 

(

@ParentMenuID int =null,

@Module varchar(10) =null,

@userId int  =null,

@OrganizationType  varchar(50) =null


)


as

begin


--select MenuID,MenuName as 'title',URL,ImageURL From PCS_MenuMaster where MainMenuId = @ParentMenuID


declare @ModuleId int,@OrganizationTypeId int


IF @Module = '' or  @Module  is null

BEGIN

SET @ModuleId=2

END

ELSE

BEGIN

SELECT @ModuleId=ModuleID FRom ModuleMaster where ModuleName = @Module

END


select  @OrganizationTypeId=OrganizationTypeId From ORG_OrganizationTypeMaster where Name = @OrganizationType



----select MenuID,MenUName as title,  ImageURL , URL, ROW_NUMBER() OVER(ORDER BY MenuID ASC) -1 AS index1  From PCS_MenuMaster  where IsDisplay = 1 and ModuleID IN(1,2) and MainMenuId = 0    order by Sequence




--select distinct MM.MenuID,MenUName as title,ImageURL,URL,Sequence  INTO #aa   FRom PCS_MenuMaster MM 

--inner join  PCS_Rights PR ON PR.SubMenuId = MM.MainMenuId

--where IsDisplay = 1  and MainMenuId = 0  and  UserId = 6  and mm.ModuleId = 2 order by Sequence



--select MenuID,title,ImageURL,URL,Sequence,ROW_NUMBER() OVER(ORDER BY MenuID ASC) -1 AS index1 FRom #aa

--Drop table #aa



Create table #MainMenu

(

   MenuID int,

   title varchar(50),

   ImageURL varchar(100),

   URL varchar(100),

   seq int ,

   radd int default 0,

   rview int default 0,

   rEdit int default 0,

   rdelete int default 0

)



insert into #MainMenu(MenuID)

select distinct OM.MenuId FRom PCS_Organization_MenuMapper OM  inner join  PCS_Rights PR

  ON PR.OrganizationTypeId = OM.OrganizationTypeId And OM.RoleId = PR.RoleId

  where UserId = @userId and OM.OrganizationTypeId = @OrganizationTypeId and OM.ModuleId = @ModuleId


update #MainMenu set title =MenUName  ,URL=a.URL,ImageURL = a.ImageURL,seq=Sequence

From PCS_MenuMaster a where a.MenuID = #MainMenu.MenuID


update #MainMenu set radd =PR.[Add] ,rview=PR.[View] ,rEdit =PR.[Edit] ,rdelete = PR.[Delete]

From PCS_Rights PR where  #MainMenu.MenuID = PR.MenuID and  PR.SubMenuID = 0



select distinct *,ROW_NUMBER() OVER(ORDER BY MenuID ASC) -1 AS index1 From #MainMenu order by MenuID

Drop table #MainMenu


-------------------------------------------------------------------------------------------------------------------------------------------------



Create table #SubMenu

(

   MenuID int,

   SubMenuID int,

   title varchar(50),

   URL varchar(100),

   isDisplay int,

   seq int,

   radd int default 0,

   rview int default 0,

   rEdit int default 0,

   rdelete int default 0

)



insert into #SubMenu(MenuID,SubMenuID)

select distinct OM.MenuID,OM.SubMenuId FRom PCS_Organization_MenuMapper OM  inner join  PCS_Rights PR

  ON PR.OrganizationTypeId = OM.OrganizationTypeId And OM.RoleId = PR.RoleId and PR.SubMenuId = OM.SubMenuId

  where UserId = @userId and OM.OrganizationTypeId = @OrganizationTypeId and OM.SubMenuId<>0 and (PR.[Add] <>0 OR PR.[View]<>0 OR PR.[Edit]<>0 OR PR.[Delete]<>0) and OM.Moduleid=@ModuleId

  --and PR.SubMenuId !=3

  --and PR.RightId = 8

  update #SubMenu set title =a.MenuName  ,URL=a.URL,seq=a.[Sequence],isDisplay = a.IsDisplay

From PCS_MenuMaster a  inner join #SubMenu ON  a.MenuID = #SubMenu.SubMenuID


update #SubMenu set radd =PR.[Add] ,rview=PR.[View] ,rEdit =PR.[Edit] ,rdelete = PR.[Delete]

From PCS_Rights PR where  #SubMenu.SubMenuID = PR.SubMenuID




select distinct *,ROW_NUMBER () OVER(Partition by  MenuID ORDER BY MenuID ASC) -1 AS index1 From #SubMenu     order by MenuID asc,isDisplay  desc


Drop table #SubMenu

--select MainMenuId as MenuID,MenUName as title, URL ,DENS_RANK() OVER(ORDER BY MenuID ASC) -1 AS index1  From PCS_MenuMaster where IsDisplay = 1 and ModuleID = 2 and MainMenuId<>0



--select distinct MainMenuId as MenuID,MenUName as title,URL,Sequence  INTO #aa1   FRom PCS_MenuMaster MM 

--inner join  PCS_Rights PR ON PR.SubMenuId = MM.MenuID

--where IsDisplay = 1  and MainMenuId <> 0  and  UserId = 6  and mm.ModuleId = 2 


--select MenuID,title,URL,Sequence,ROW_NUMBER() OVER(ORDER BY MenuID ASC) -1 AS index1 FRom #aa1


--Drop table #aa1


--select MainMenuId as MenuID,MenUName as title, URL   From PCS_MenuMaster where IsDisplay = 1 and ModuleID = 2 and MainMenuId = @ParentMenuID order by Sequence

--select * From PCS_MenuMaster where IsDisplay = 1








select distinct ModuleName FRom PCS_Rights PR inner join ModuleMaster MM ON

MM.ModuleID = PR.ModuleId

--where pr.ModuleId=@ModuleId

end






--elect * from PCS_MenuMaster

  private void PopulateMenu(DataSet ds)

        {


            string currentPage = Path.GetFileName(Request.Url.AbsolutePath);


            List<MenuMasterList> MenuList = new List<MenuMasterList>();

            List<MenuMasterList> subMenuList = new List<MenuMasterList>();

            DataTable Child = new DataTable();


            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)

            {

                MenuMasterList obj = new MenuMasterList();

               string currentPage1 = Convert.ToString(ds.Tables[0].Rows[i]["URL"]);

                int MenuID = Convert.ToInt32(ds.Tables[0].Rows[i]["MenuID"]);

                DataView dvSubMenu = new DataView(ds.Tables[1]);

                dvSubMenu.RowFilter = "MenuID= '" + MenuID + "'";

                if (dvSubMenu.Count > 0)

                {


                    DataTable tblFiltered = ds.Tables[1].AsEnumerable()

                                 .Where(r => r.Field<Int32>("MenuID") == MenuID)

                                 .Where(r => r.Field<Int32>("IsDisplay") == 1)

                                 .CopyToDataTable();


                    obj.MenuID = Convert.ToInt32(tblFiltered.Rows[0]["MenuID"]);

                    obj.Title = Convert.ToString(ds.Tables[0].Rows[i]["Title"]);

                    obj.URL = Convert.ToString(tblFiltered.Rows[0]["URL"]);

                    obj.ImageURL = Convert.ToString(ds.Tables[0].Rows[i]["ImageURL"]);

                    MenuList.Add(obj);


                }

                else

                {




                    obj.MenuID = Convert.ToInt32(ds.Tables[0].Rows[i]["MenuID"]);

                    obj.Title = Convert.ToString(ds.Tables[0].Rows[i]["Title"]);

                    obj.URL = Convert.ToString(ds.Tables[0].Rows[i]["URL"]);

                    obj.ImageURL = Convert.ToString(ds.Tables[0].Rows[i]["ImageURL"]);

                    MenuList.Add(obj);

                }


            }




            DataView dvMenu = new DataView(ds.Tables[0]);

            dvMenu.RowFilter = "URL Like '%" + currentPage.Remove(currentPage.Length - 5) + "%'";

            if (dvMenu.Count > 0)

            {

                DataTable dtemp = dvMenu.ToTable();

                int index = Convert.ToInt32(dtemp.Rows[0]["index1"]);

                int MenuID = Convert.ToInt32(dtemp.Rows[0]["MenuID"]);



                DataView dv22 = new DataView(ds.Tables[1]);

                dv22.RowFilter = "URL Like '%" + currentPage.Remove(currentPage.Length - 5) + "%'";

                DataTable dvemp1 = dv22.ToTable();

                if (dvemp1.Rows.Count > 0)

                {


                }



                LstMainMenu.SelectedIndex = index;

                Rights(dtemp);

            }

            else

            {

                DataView dvSubMenu = new DataView(ds.Tables[1]);

                dvSubMenu.RowFilter = "URL Like '%" + currentPage.Remove(currentPage.Length - 5) + "%'";

                if (dvSubMenu.Count > 0)

                {

                    DataTable dtemp = dvSubMenu.ToTable();


                    int MenuID = Convert.ToInt32(dtemp.Rows[0]["MenuID"]);


                    DataTable tblFiltered = ds.Tables[0].AsEnumerable()

                                 .Where(r => r.Field<Int32>("MenuID") == MenuID)

                                 .CopyToDataTable();


                    int index = Convert.ToInt32(tblFiltered.Rows[0]["index1"]);

                    LstMainMenu.SelectedIndex = index;

                    Rights(dtemp);


                }

            }


            LstMainMenu.DataSource = MenuList;

            LstMainMenu.DataBind();


         

            DataView dv = new DataView(ds.Tables[1]);

            dv.RowFilter = "URL Like '%" + currentPage.Remove(currentPage.Length - 5) + "%'";

            DataTable dvemp = dv.ToTable();


            if (dvemp.Rows.Count > 0)

            {

                Rights(dvemp);

                int menuid = Convert.ToInt32(dvemp.Rows[0]["MenuID"]);

                int subindex = Convert.ToInt32(dvemp.Rows[0]["index1"]);


                DataView dvMenu1 = new DataView(ds.Tables[0]);

                dvMenu1.RowFilter = "MenuID = '" + Convert.ToString(menuid) + "'";


                if (dvMenu1.Count > 0)

                {

                    DataTable dtemp = dvMenu1.ToTable();

                    int index = Convert.ToInt32(dtemp.Rows[0]["index1"]);

                    LstMainMenu.SelectedIndex = index;

               }


                DataTable tblFiltered = ds.Tables[1].AsEnumerable()

                                 .Where(r => r.Field<Int32>("MenuID") == menuid)

                                 .Where(r => r.Field<Int32>("IsDisplay") == 1)

                                 .CopyToDataTable();


                if (tblFiltered.Rows.Count > 0)

                {

                    for (int i = 0; i < tblFiltered.Rows.Count; i++)

                    {

                        MenuMasterList obj = new MenuMasterList();

                        obj.MenuID = Convert.ToInt32(tblFiltered.Rows[i]["MenuID"]);

                        obj.Title = Convert.ToString(tblFiltered.Rows[i]["Title"]);

                        obj.URL = Convert.ToString(tblFiltered.Rows[i]["URL"]);

                        subMenuList.Add(obj);

                    }

                    LstSubMenu.SelectedIndex = subindex;



                    LstMainMenu.DataSource = MenuList;

                    LstMainMenu.DataBind();


                    LstSubMenu.DataSource = subMenuList;

                    LstSubMenu.DataBind();

                }


            }

            else

            {

                LstMainMenu.DataSource = MenuList;

                LstMainMenu.DataBind();

            }

        }





.aspx



     <div class="liquid-nav">

                <asp:ListView runat="server" ID="LstMainMenu" OnItemDataBound="LstMainMenu_ItemDataBound">

                    <LayoutTemplate>

                        <ul>

                            <li id="itemPlaceholder" runat="server"></li>

                            

                        </ul>

                    </LayoutTemplate>

                    <ItemTemplate>

                        <li>

                            <div>

                                <img src='<%# Eval("imageUrl")  %>' alt='<%# Eval("title") %>'

                                    title='<%# Eval("title") %>' align="absbottom" /> 

                                <a href='<%# Page.ResolveClientUrl((string)Eval("URL"))%>'><%# Eval("title") %></a>



                            </div>

                        </li>

                    </ItemTemplate>

                    <SelectedItemTemplate>

                        <li class="selected">

                            <div>

                                <img src='<%# Eval("imageUrl")  %>' alt='<%# Eval("title") %>'

                                    title='<%# Eval("title") %>' align="absbottom" /><%# Eval("title") %>

                            </div>

                        </li>

                    </SelectedItemTemplate>

                </asp:ListView>

            </div>

            <div id="liquid-bd" class="pink-bg" style="min-height: 500px; border-top: 4px solid rgb(160, 204, 57);">

                <div class="input-content">

                    <div class="top-links">

                        <asp:ListView runat="server" ID="LstSubMenu" OnItemDataBound="LstSubMenu_Databound">

                            <LayoutTemplate>

                                <ul>

                                    <li id="itemPlaceholder" runat="server"></li>

                                </ul>

                            </LayoutTemplate>

                            <ItemTemplate>

                                <asp:Panel ID="lbtittle" runat="server">

                                    <li class='<%# Container.DataItemIndex ==  (LstSubMenu.DataSource as System.Collections.Generic.List<MenuMasterList>).Count -1 ? "sep-none" : null %>' id="liMenu" runat="server">

                                        <a href='<%# Page.ResolveClientUrl((string)Eval("URL"))%>'>

                                            <asp:Label ID="lbtidsdttle" runat="server" Text='<%# Eval("title") %>'></asp:Label></a> </li>

                                    <%-- <%# Eval("title") %></a> </li>--%>

                                </asp:Panel>

                            </ItemTemplate>

                            <SelectedItemTemplate>

                                <asp:Panel ID="lbtittles" runat="server">

                                    <li class='<%# Container.DataItemIndex ==  (LstSubMenu.DataSource as System.Collections.Generic.List<MenuMasterList>).Count -1 ? "active sep-none" : "active" %>'>

                                        <asp:Label ID="lbtittldses" runat="server" Text='<%# Eval("title") %>'></asp:Label>

                                    </li>

                                    <%--   <%# Eval("title") %></li>--%>

                                </asp:Panel>

                            </SelectedItemTemplate>

                        </asp:ListView>

                        <div class="clear">

                        </div>

                    </div>

                    <div class="clear">

                        <asp:ContentPlaceHolder ID="hldPageHeader" runat="server">

                        </asp:ContentPlaceHolder>

                        <div id="divLinks" runat="server">

                            <div id="divCollapsed" style="display: none; width: 1px; margin-right: 10px;" class="right-col">

                                <div class="expand-col">

                                    <div class="minimise-icon">

                                        <a href="#">

                                            <img src="../images/maximise_icon.png" id="exp_img" alt="Expand" title="Expand" onclick="exp_click(this.id,1)" /></a>

                                    </div>

                                </div>

                            </div>

                            <div class="right-col" id="divRightPan" style="display: none;">

                                <div class="expand-col">

                                    <div class="minimise-icon">

                                        <a href="#">

                                            <img src="../images/minimise_icon.png" id="exp_img" alt="Collapse" title="Collapse"

                                                onclick="exp_click(this.id,1)" /></a>

                                    </div>

                                    <div class="gray-box-5">

                                        <div class="gray-box-1">

                                            <%--<div class="shadow">

                                            <div class="display-content">

                                                asdf asdf asdf asdf

                                            </div>

                                        </div>--%>

                                        </div>

                                    </div>

                                    <div class="gray-box-10">

                                        <div class="gray-box">

                                            <div class="header">

                                                <div class="left">

                                                    <h2 style="color: Green;">Quick Links

                                                    </h2>

                                                </div>

                                            </div>

                                            <div class="content12 blue-bullets">

                                                <asp:Repeater ID="rptParent" runat="server" OnItemDataBound="rptParent_ItemDataBound">

                                                    <ItemTemplate>

                                                        <asp:Repeater ID="rptTracking" runat="server">

                                                            <HeaderTemplate>

                                                                <fieldset style="border-width: 0.2em;">

                                                                    <legend align="center" style="color: #BD3F0F; font-weight: bold;">

                                                                        <%# ((RepeaterItem)Container.Parent.Parent).DataItem.ToString() %></legend>

                                                                    <ul>

                                                            </HeaderTemplate>

                                                            <ItemTemplate>

                                                                <%--<ul>--%>

                                                                <li><a href='<%#Eval("value") %>' target="<%#Eval("target") %>">

                                                                    <%# Eval("text")%></a></li>

                                                                <%--</ul>--%>

                                                            </ItemTemplate>

                                                            <FooterTemplate>

                                                                </ul> </fieldset>

                                                            </FooterTemplate>

                                                        </asp:Repeater>

                                                    </ItemTemplate>

                                                </asp:Repeater>

                                            </div>

                                        </div>

                                    </div>

                                    <%--  <div align="center">

                                    <img src="../images/quick_tour_img.gif" alt="Airlines" title="FedEx" /></div>--%>

                                </div>

                            </div>

                        </div>

                        <%-- <div class="marq-ee" id="GSTDetails" runat="server" visible="false">

                         <marquee><asp:LinkButton ID="lnkGstDetails" runat="server" OnClick="lnkGstDetails_Click" Text="**** Request to update GST Details in your organization profile before 1-July-2017. Please click here to Update the same. ****" style="color:#d34b17;font-size:12px;font-weight:bold;"></asp:LinkButton></marquee> 

                        </div>--%>


                        <div class="left-col" id="divLeftPan" runat="server">

                            <asp:ContentPlaceHolder runat="server" ID="hldPage" />

                        </div>

                    </div>

                </div>

            </div>




last :- output





Popular posts from this blog

Add barcode on pdf

using System; using System.IO; using System.Drawing; using ZXing; using iTextSharp.text; using iTextSharp.text.pdf; using System.Drawing; using System.Web.UI.WebControls; namespace PDFBarcode {     public partial class WebForm1 : System.Web.UI.Page     {         byte[] byteImage;         protected void Page_Load(object sender, EventArgs e)         {         }         protected void btnDownload_Click(object sender, EventArgs e)         {                       Random r = new Random();             string fileName = r.Next().ToString();             string filenameext = fileName + ".pdf";             GenerateBarcode(fileName);             string Sourcepath = Server.MapPath("~/SavedF...