多級下拉菜單(動態(tài))


          -------------------------------------
          源代碼下載/Files/singlepine/topmenu.rar

          public class TopMenu : System.Web.UI.Page
          {
              protected DataRow[] father;
              protected DataRow[] first;
              protected DataRow[] second;
                  private void Page_Load(object sender, System.EventArgs e)
                  {
                      
                  }
              public static string ConnectionString=System.Configuration .ConfigurationSettings .AppSettings["ConnectionString"];
              
                  GetDataSet#region GetDataSet
                  public static DataSet GetDataSet(string sql)
                  {
                      SqlDataAdapter    sda =new SqlDataAdapter(sql,ConnectionString);
                      DataSet ds=new DataSet();
                      sda.Fill(ds);
                      return ds;
                  }
                  #endregion
                  Web Form Designer generated code#region Web Form Designer generated code
                  override protected void OnInit(EventArgs e)
                  {
                      //
                      // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                      //
                      InitializeComponent();
                      base.OnInit(e);
                  }
                  
                  /**//**//**//// <summary>
                  /// Required method for Designer support - do not modify
                  /// the contents of this method with the code editor.
                  /// </summary>
                  private void InitializeComponent()
                  {    
                      this.Load += new System.EventHandler(this.Page_Load);
                  }
                  #endregion
              LoadTopMenu#region LoadTopMenu
              protected string LoadTopMenu()
              {
                  //IsBoot設(shè)置菜單級別,0一級,1二級,2三級,依此類推.
                  string sqlFather="select * from topmenu order by IsBoot";
                  DataSet dsFather=GetDataSet(sqlFather);
                  father=dsFather.Tables[0].Select("IsBoot=0","IsBoot");
                  string menu="";            
                  int one=0;
                  int two=1;
                  int three=1;
                  foreach(DataRow drfather in father)
                  {                
                      menu+="mpmenu"+one+"=new mMenu("+"'"+drfather["text"]+"'"+",'/','self','','','','');";
                      first=dsFather.Tables[0].Select("ParentID='"+Convert.ToInt32(drfather["ID"])+"' and IsBoot=1","IsBoot");
                      foreach(DataRow drfirst in first)
                      {
                          second=dsFather.Tables[0].Select("ParentID='"+Convert.ToInt32(drfirst["ID"])+"' and IsBoot=2","IsBoot");
                          if(second.Length==0)
                          {
                              menu+="mpmenu"+one+".addItem(new mMenuItem("+"'"+drfirst["text"]+"'";//description
                              menu+=","+"'"+drfirst["url"]+"'"+",";//url
                              menu+="'"+drfirst["target"]+"'"+",";//target
                              menu+=""+drfirst["visible"]+",";//是否可見,false可見,true不可見
                              menu+="'"+drfirst["status"]+"'"+",";//狀態(tài)條
                              menu+="null,'','','',''));";
                          }
                          foreach(DataRow drsecond in second)
                          {    
                              menu+="msub"+two+"=new mMenuItem("+"'"+drfirst["text"]+"','',"+"'"+drfirst["target"]+"'";
                              menu+=","+drfirst["visible"]+",";//是否可見,false可見,true不可見
                              menu+="'','1','','','','');";

                              menu+="msub"+three+".addsubItem(new mMenuItem("+"'"+drsecond["text"]+"'";//description
                              menu+=","+"'"+drsecond["url"]+"'"+",";//url
                              menu+="'"+drsecond["target"]+"'"+",";//target
                              menu+=""+drsecond["visible"]+",";//是否可見,false可見,true不可見
                              menu+="'"+drsecond["status"]+"'"+",";//狀態(tài)條
                              menu+="null,'','','',''));";
                              menu+="mpmenu"+one+".addItem(msub"+two+");";//addItem
                              three++;
                          }
                          two++;
                      }
                      one++;
                  }
                  return menu;
              }
              #endregion
          }

          3.數(shù)據(jù)庫腳本
           

          --建表
          if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[topmenu]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
          drop table [dbo].[topmenu]
          GO

          CREATE TABLE [dbo].[topmenu] (
              [ID] [int] NOT NULL ,
              [parentId] [int] NOT NULL ,
              [text] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL ,
              [url] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL ,
              [target] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
              [IsBoot] [int] NULL ,
              [visible] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
              [status] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL 
          ) ON [PRIMARY]
          GO

          --插入測試數(shù)據(jù)
          insert into topmenu([ID],[parentId],[text],[url],[target],[IsBoot],[visible] ,[status])
          values(1,0,'深圳','http://singlepine.cnblogs.com/articles/259955.html','self',0,'false','深圳')

          insert into topmenu([ID],[parentId],[text],[url],[target],[IsBoot],[visible] ,[status])
          values(2,1,'南山','http://singlepine.cnblogs.com/articles/259955.html','self',1,'false','南山')

          insert into topmenu([ID],[parentId],[text],[url],[target],[IsBoot],[visible] ,[status])
          values(3,1,'福田','http://singlepine.cnblogs.com/articles/259955.html','self',1,'false','福田')

          insert into topmenu([ID],[parentId],[text],[url],[target],[IsBoot],[visible] ,[status])
          values(4,2,'科技園','http://singlepine.cnblogs.com/articles/259955.html','self',2,'false','科技園')

          insert into topmenu([ID],[parentId],[text],[url],[target],[IsBoot],[visible] ,[status])
          values(5,3,'塞格','http://singlepine.cnblogs.com/articles/259955.html','self',2,'false','塞格')

          insert into topmenu([ID],[parentId],[text],[url],[target],[IsBoot],[visible] ,[status])
          values(6,0,'廣州','http://singlepine.cnblogs.com/articles/259955.html','self',0,'false','廣州')

          insert into topmenu([ID],[parentId],[text],[url],[target],[IsBoot],[visible] ,[status])
          values(7,6,'廣州火車站','http://singlepine.cnblogs.com/articles/259955.html','self',1,'false','廣州火車站')

          posted on 2007-06-20 13:32 chenguo 閱讀(671) 評論(0)  編輯  收藏 所屬分類: 組件 標簽使用

          <2025年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          導航

          統(tǒng)計

          留言簿

          隨筆分類(1)

          文章分類(52)

          好友 小山的博客

          最新隨筆

          最新評論

          主站蜘蛛池模板: 将乐县| 柳州市| 华阴市| 陕西省| 义乌市| 禹城市| 肥东县| 阜宁县| 南平市| 公主岭市| 东阿县| 满城县| 昌宁县| 钦州市| 临海市| 新邵县| 调兵山市| 延津县| 崇文区| 贵定县| 项城市| 承德市| 新泰市| 图木舒克市| 敦化市| 利辛县| 崇州市| 鄂托克旗| 渝中区| 鸡泽县| 绥棱县| 安徽省| 镇赉县| 环江| 游戏| 武威市| 宁强县| 石阡县| 揭阳市| 柘荣县| 房产|