ASP.NET数据绑定菜单控件

 动态绑定MENU控件:

public void BindMenu(Menu mn,MenuItem menu,string mainMenu,string sql) 
        { 
                MenuItem mitem = null; 
                DB db = new DB(); 
                DataSet ds = db.GetDataSet(sql, "table1"); 
                DataTable dTable = ds.Tables[0]; 
                DataRow[] drow = dTable.Select(" parentId='" + mainMenu + "'"); 
                 
                foreach (DataRow dr in drow) 
                { 
                        mitem = new MenuItem(); 
                        mitem.Text = dr["modulName"].ToString(); 
                        mitem.Value = dr["modulId"].ToString(); 
                        mitem.NavigateUrl = "Default.aspx"; 
                        if (menu == null) 
                        { 
                                mn.Items.Add(mitem); 
                        } 
                        else 
                        { 
                                menu.ChildItems.Add(mitem); 
                        } 
                        DataSet dst = db.GetDataSet(sql, "table2"); 
                        DataTable dtb = dst.Tables[0]; 
                        DataRow[] dar = dtb.Select(" parentId='" + mitem.Value + "'"); 
                        mainMenu = mitem.Value; 
                        BindMenu(Menu1, mitem, mainMenu, sql); 
                } 
        }


动态绑定树控件:

public void BindTree(TreeView tview, TreeNode tn_main, string parentId,string sql) 
        { 
                TreeNode tn=null; 
                tview.Font.Size = FontUnit.Small; 
                tview.ForeColor = System.Drawing.Color.Black; 
                DB db = new DB(); 
                DataSet ds = db.GetDataSet(sql, "table1"); 
                DataTable dTable = ds.Tables[0]; 
                DataRow[] drows = dTable.Select(" parentId='"+parentId+"'"); 
                foreach (DataRow drow in drows) 
                { 
                        tn = new TreeNode(); 
                        tn.Text = drow["modulName"].ToString(); 
                        tn.Value = drow["modulId"].ToString(); 
                        tn.SelectAction = TreeNodeSelectAction.Expand; 
                        tn.NavigateUrl = "Default2.aspx"; 
                        tn.Expanded = true; 
                        if (tn_main == null) 
                        { 
                                tview.Nodes.Add(tn); 
                        } 
                        else 
                        { 
                                tn_main.ChildNodes.Add(tn); 
                        } 
                        DataSet dst = db.GetDataSet(sql, "table2"); 
                        DataTable dtb = dst.Tables[0]; 
                        DataRow[] dar = dtb.Select(" parentId='" + tn.Value + "'"); 
                        parentId = tn.Value; 
                        BindTree(tview,tn, parentId,sql); 
                } 
        }


 

你可能感兴趣的:(sql,String,table,asp.net,menu,dataset)