Click here to Skip to main content
15,904,023 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am using menu control and i want to hide the menu's based on my sql condition.

Initially i am loading all the menu's in the design mode.

while page load and based on my sql condition i am removing the menu's from the list.

I want, when i remove one menu in master then the entire master and master child menu should be disappear.

When i remove one meni in transaction then entire transaction and child transaction menu should be disappear.

how to do this..

see my code
------------
ASPX Page
----------
ASP.NET
<div id="menu" class="nav">
                            <asp:Menu ID="NavigationMenu" runat="server" Orientation="Horizontal" RenderingMode="List">            
                                <Items>
                                    <asp:MenuItem NavigateUrl="~/Home.aspx"  Text="Home" Value="Home"  />
                                    <asp:MenuItem  Text="Master" Value="Master"> 
                                            <asp:MenuItem  Text="Master_Child1" Value="Master_Child1"></asp:MenuItem> 
                                            <asp:MenuItem Text="Master_Child2" Value="Master_Child2"></asp:MenuItem> 
                                            <asp:MenuItem  Text="Master_Child3" Value="Master_Child3"></asp:MenuItem> 
                                    </asp:MenuItem> 
                                    <asp:MenuItem  Text="Transaction" Value="Tran"> 
                                            <asp:MenuItem  Text="Tran_Child1" Value="Tran_Child1"></asp:MenuItem> 
                                            <asp:MenuItem Text="Tran_Child2" Value="Tran_Child2"></asp:MenuItem> 
                                    </asp:MenuItem> 
                                     <asp:MenuItem  Text="Reports" Value="Report"> 
                                            <asp:MenuItem  Text="Reports" Value="Reports"></asp:MenuItem> 
                                            <asp:MenuItem  Text="Report_Child1" Value="Report_Child1"></asp:MenuItem> 
                                    </asp:MenuItem> 
                                    <asp:MenuItem NavigateUrl="~/Others.aspx"  Text="Others" Value="Others"  />
                                </Items>
                            </asp:Menu>
                        </div>

ASPX.VB Code
-------------
--------------
VB
NavigationMenu.Items.Clear()
       Conn.Open()
       StrQry = ""
       StrQry = " Select isnull(Home,'') as Admin,isnull(Master_Child1,'') as Master_Child1,isnull(Master_Child2,'') as Master_Child2,"
       StrQry &= " isnull(Tran_Child1,'') as Tran_Child1,isnull(Tran_Child2,'') as Tran_Child2,"
       StrQry &= " isnull(Report_Child1,'') as Report_Child1,isnull(Report_Child2,'') as Report_Child2,"
       StrQry &= " isnull(Others,'') as Others"
       Cmd.Connection = Conn
       Cmd.CommandText = StrQry
       Rdr = Cmd.ExecuteReader
       If Rdr.HasRows Then
           While Rdr.Read
                       If Rdr("Home") = "N" Then
                           Dim mnuItems As New MenuItem()
                           NavigationMenu.Items.Remove(mnuItems)
                       End If
                   If Rdr("Master_Child1") = "N" Then
                       Dim mnuItems As New MenuItem()
                       NavigationMenu.Items.Remove(mnuItems)
                   End If
           If Rdr("Master_Child2") = "N" Then
                       Dim mnuItems As New MenuItem()
                       NavigationMenu.Items.Remove(mnuItems)
                   End If
                   ---------------
                   ---------------
                   ---------------
                   ---------------
            End While
       End if


How to disappear the parent and child menu while all the child menu do not have rights...?
Posted
Updated 13-Dec-11 2:55am
v4

1 solution

Set the Selectable & Enabled property false.

I had one situation where i users had rights according to their department.
I had created one table User_Rights. Their I created one field Module_Name. Value in this field is similar to menu items value.

Here is the code

Dim i As Integer = 0
While i < Menu1.Items.Count
If Menu1.Items(i).ChildItems.Count > 0 Then
Dim j As Integer = 0
For j = 0 To Menu1.Items(i).ChildItems.Count - 1
If Menu1.Items(i).ChildItems(j).ChildItems.Count > 0 Then
Dim k As Integer = 0
Do While k < Menu1.Items(i).ChildItems(j).ChildItems.Count
iRow = 0
bFound = False
For iRow = 0 To dt.Rows.Count - 1
drow = dt.Rows(iRow)
If drow("Module_Name") = Menu1.Items(i).ChildItems(j).ChildItems(k).Value.ToString() Then
bFound = True
Exit For
End If
Next
If bFound = False Then
Menu1.Items(i).ChildItems(j).ChildItems(k).Enabled = False
Menu1.Items(i).ChildItems(j).ChildItems(k).Selectable = False
End If
k = k + 1
Loop
Else
iRow = 0
bFound = False
For iRow = 0 To dt.Rows.Count - 1
drow = dt.Rows(iRow)
If drow("Module_Name") = Menu1.Items(i).ChildItems(j).Value.ToString Then
bFound = True
Exit For
End If
Next
If bFound = False Then
Menu1.Items(i).ChildItems(j).Enabled = False
Menu1.Items(i).ChildItems(j).Selectable = False
End If
End If
Next
End If
i += 1
End While
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900