Hi All,
I am using VWD 2010 Express with mySQL as database. I have a treeview menu which i want to populate as per the permissons/access set. I took ideas/codes from these links
http://aspalliance.com/732
http://www.codeproject.com/Articles/366450/Permissions-and-Levels-in-ASP-Menu
My treeview is as below:
<div id="MainMenu" style="height: 600px;">
<asp:TreeView ID="tvwMainMenu" runat="server" ExpandDepth="0" PopulateNodesFromClient="true"
ShowLines="true" ShowExpandCollapse="true" SelectedNodeStyle-BackColor="#8E909C"
SelectedNodeStyle-BorderColor="Black" HoverNodeStyle-BackColor="#7E909C" HoverNodeStyle-BorderColor="Black"
ImageSet="BulletedList4">
</asp:TreeView>
</div>
My menu structure and user permission table and data is as below
Menu Structure
ID nID nName Path
3 0 Orders
1 0 Masters
2 0 Quotes
4 1 System Master system.aspx
5 1 Clients Master clients.aspx
6 1 Agents Master agents.aspx
7 2 New Orders neworders.aspx
8 2 Pending Orders pendingorders.aspx
9 3 Estimates estimates.aspx
10 3 Estimate Lists estimatelists.aspx
User Permissions
uID Password uLevel
aa ad 1-2-3-4-5-6-7-8-9-10
uu us 1-2-4-5-6-9-10
What i want is that the treeview should be populated according to the structure set in the Users Permissions table
My codebehind is as below:
Dim permissions()AsStringProtectedSubPage_Load(ByVal sender AsObject,ByVal e AsSystem.EventArgs)HandlesMe.LoadGetConnDetails()IfNotPage.IsPostBackThenIfNotSession("UName")=""ThenLoadMenu()EndIfLoadRootMenu()EndIfPrivateSubGetConnDetails()Dim conn AsNewMySql.Data.MySqlClient.MySqlConnectionDim myConnectionString AsString myConnectionString ="server=127.0.0.1; uid=ua;pwd=pa;database=db;"Try conn.ConnectionString= myConnectionString conn.Open()Catch ex AsMySql.Data.MySqlClient.MySqlExceptionMsgBox(ex.Message)EndTryEndSubPrivateSubLoadMenu()IfSession("UName")=""ThenReturnEndIfDim strPermissionString AsString strPermissionString = getUserPermissions(Session("UName").ToString()) permissions = strPermissionString.Split("-")EndSubPrivateFunction getUserPermissions(ByVal strUserName AsString)AsStringDim myDataSet AsNewDataSetDim dt AsNewDataTableDim objConn AsNewMySqlConnection("server=127.0.0.1; user id=ua; password=pa; database=db; pooling=false;")Dim objCommand AsNewMySqlCommand("SELECT * FROM users where NameOfUser='"&Session("UName")&"'", objConn)Dim da AsNewMySqlDataAdapter(objCommand) da.Fill(myDataSet,"UserDetails")If myDataSet.Tables("UserDetails").Rows.Count=0ThenScriptManager.RegisterStartupScript(Me,Me.GetType(),"not found!","alert('User not Found!!')",True)Return""EndIf objConn.Close()Return myDataSet.Tables("UserDetails").Rows(0)("uLevel").ToString()EndFunctionPublicSubPopulateRootLevel()Dim tvwMain AsNewTreeView tvwMain =Me.FindControl("tvwMain")Dim myDataSet AsNewDataSetDim dt AsNewDataTableDim objConn AsNewMySqlConnection("server=localhost; user id=ua; password=pa; database=db; pooling=false;")Dim strTemp AsString strTemp ="SELECT nID, nName, (SELECT COUNT(*) FROM Menu sc WHERE sc.nID = nID) childnodecount FROM Menu "ForEach childID In permissionsIfIsNumeric(childID)=FalseThenExitForEndIfIfIndexOf(permissions, childID)=0Then strTemp +=" WHERE ID = "+ childIDElse strTemp +=" OR ID = "+ childIDEndIfNext strTemp +=" ORDER BY NodeID"MsgBox(strTemp)Dim objCommand AsNewMySqlCommand(strTemp, objConn)Dim da AsNewMySqlDataAdapter(objCommand) da.Fill(dt)PopulateNodes(dt, tvwMain.Nodes)EndSubPrivateSubPopulateNodes(ByVal dt AsDataTable,ByVal nodes AsTreeNodeCollection)ForEach dr AsDataRowIn dt.RowsDim tn AsNewTreeNode() tn.Text= dr("nName").ToString() tn.Value= dr("nID").ToString() nodes.Add(tn) tn.PopulateOnDemand=(CInt(dr("childnodecount"))>0)NextEndSubPrivateSubPopulateSubLevel(ByVal parentid AsInteger,ByVal parentNode AsTreeNode)' Dim objConn As New MySqlConnection("server=localhost; user id=ua; password=pa; database=db; pooling=false;") 'Dim objCommand AsNewMySqlCommand("select id,nName,(select count(*) FROM Menu " _' & "WHERE nID=sc.id) childnodecount FROM MainMenu sc where nID=@parentID", objConn) ' objCommand.Parameters.AddWithValue("@parentID",SqlDbType.Int).Value= parentid' Dim da As New MySqlDataAdapter(objCommand) 'Dim dt AsNewDataTable()' da.Fill(dt) 'PopulateNodes(dt, parentNode.ChildNodes)EndSubProtectedSub tvwMainMenu_TreeNodePopulate(ByVal sender AsObject,ByVal e AsSystem.Web.UI.WebControls.TreeNodeEventArgs)Handles tvwMainMenu.TreeNodePopulate'PopulateSubLevel(CInt(e.Node.Value), e.Node) End Sub
I have commented some bits in order to first populate the root nodes (Orders, Masters, Quotes etc.) and then put permissible sub nodes under each root node.
The code above is not working correctly and all nodes are being populated as root nodes. Where am i going wrong? I will try to add the navigation link later but if it can be included in the correct solution, I will appreciate.
Thanks in advance
Tom