DropDownList包含樹型菜單

 private  void  Page_Load(object  sender,  System.EventArgs  e)  
                       {  
                                   //  在此處放置用戶代碼以初始化頁面  
                                   if(!Page.IsPostBack)  
                                   {  
                                               SqlConnection  conn=new  SqlConnection(ConfigurationSettings.AppSettings[  "conn  "]);  
                                               SqlDataAdapter  da=new  SqlDataAdapter(  "select  *  from  TreeView  ",conn);  
                                               DataSet  ds=new  DataSet();  
                                               da.Fill(ds);  
                                               bindDDL(ddl,ds,0,  "  ");  
                                   }  
                       }  
                       private  void  bindDDL(DropDownList  DDL,DataSet  ds,int  ParentID,string  Tab)  
                       {  
                                   DataView  dv=new  DataView(ds.Tables[0]);  
                                   dv.RowFilter=  "ParentMenu=  "+ParentID.ToString();  
                                   int  count=0;  
                                   foreach(DataRowView  Row  in  dv)    
                                   {  
                                               count++;  
                                               if(Tab.IndexOf(  "├  ")  >-1)  
                                               {  
                                                           Tab=Tab.Replace(  "├  ",  "│  ");  
                                               }  
                                               if(Tab.IndexOf(  "└  ")  >-1)  
                                               {  
                                                           Tab=Tab.Replace(  "└  ",  "  ");  
                                               }  
                                               if(dv.Count==count)  
                                               {  
                                                           Tab  =Tab+  "└  ";  
                                               }  
                                               else    
                                               {  
                                                           Tab  =Tab+  "├  ";  
                                               }  
                                               DDL.Items.Add(new  ListItem(Tab+Row[  "MenuName  "].ToString(),Row[  "MenuID  "].ToString()));  
                                               bindDDL(ddl,ds,int.Parse(Row[  "MenuID  "].ToString()),Tab);  
                                               Tab=  Tab.Remove(Tab.Length-1,1);  
                                   }  
 
                       }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章