无限级菜单在Label或者span中的实现

这是我自己摸索了好几天才弄出来的无限级下拉菜单,之前自己解决了DropdownList控件中显示的无限级下拉列表菜单, 在我做wap网站时碰到了这个问题,困绕了我不少天,到处发求救信,在QQ群呀,CSDN呀,都发了,看了不少的资料,但最后还是没有通过这些方式解决. 我就想,这是怎么回事呢,这个问题几乎是搞这行的都要碰到的,怎么就没人能给我直接的解决答案呢.  后天我自己再回顾一下过程,仔细想一下算法思路,最后给解决了. 其实这个问题对于高手来说,是很容易解决了. 这只适合于我这种刚入门的家伙.  呵呵,话不多说了,下面来看看吧.

这是我的前台页面,我懒,没有去整理得清析些提供给大家,就把整个文件copy过来了,虽然如此,但这对于新手来说估计也是一种更好理解的帮助.   文件名是:__Left.aspx

<%@ Page language="c#" Codebehind="__Left.aspx.cs" AutoEventWireup="false" Inherits="Wap.Admin.Resource.___Left" %>
<%@ Import namespace="System.Data" %>
<%@ Import namespace="System.Data.SqlClient" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>左边部分</title>
  <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=gb2312">
  <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
  <meta name="CODE_LANGUAGE" Content="C#">
  <meta name="vs_defaultClientScript" content="JavaScript">
  <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
  <LINK rel="stylesheet" type="text/css" href="../../CSS/Admin_StyleSheet.css">
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
   <table width="98%" border="0" align="center" cellpadding="4" cellspacing="1">
    <tr>
     <td colspan="2"><div align="center">
       <asp:Label id="lblSortName" runat="server" ForeColor="Red">Label</asp:Label></div>
     </td>
    </tr>
    <tr class="topbg">
     <td><div align="center"><a href="Source_AddClass.aspx?Source_ID=<% = Request.QueryString["Source_ID"] %>" target="Source_main">添加分类</a></div>
     </td>
     <td><div align="center"><a href="Source_ManageClass.aspx?Source_ID=<% = Request.QueryString["Source_ID"] %>" target="Source_main">管理分类</a></div>
     </td>
    </tr>
    <tr class="tdbg">
     <td colspan="2"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
                      <tr>
                        <td>
      <asp:Label ID="Label1" runat="server" />
      </td>
                      </tr>
                    </table></td>
    </tr>
   </table>
  </form>
 </body>
</HTML>
后台页面的代码,__Left.aspx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Web.Security;
using System.Configuration;

namespace Wap.Admin.Resource
{
 /// <summary>
 /// ___Left 的摘要说明。
 /// </summary>
 public class ___Left : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.Label lblSortName;


  protected System.Data.SqlClient.SqlConnection SqlConn;
  protected System.Data.SqlClient.SqlCommand SqlComm;
  protected System.Web.UI.HtmlControls.HtmlGenericControl spanShowMenuList;
  protected System.Web.UI.WebControls.Label Label1;

 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 没有登录,跳转到指定页面
   if (Session["WapAdmin"] == null)
   {
    Response.Redirect("../../Inc/Error.aspx?DescriptionType=0&ErrDescription=您没有登录,请先登录&LocationBack=../Admin/Index.aspx");
    return;
   }

   // 在此处放置用户代码以初始化页面
   SqlConn        = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["SqlConn"]);

   // 读取栏目中文名称值====================================begin
   SqlComm        = new System.Data.SqlClient.SqlCommand("Select Source_CName From Resources Where Source_ID = " + Request.QueryString["Source_ID"] + "",SqlConn);
   SqlDataReader SqlDr;
   SqlComm.Connection.Open();
   SqlDr        = SqlComm.ExecuteReader();
   SqlDr.Read();

   lblSortName.Text     = SqlDr["Source_CName"].ToString();  //返回lblSortName这个标签的值

   SqlDr.Close();
   SqlComm.Connection.Close();
   // 读取栏目中文名称值====================================end


   ShowMenuList(Int32.Parse(Request.QueryString["Source_ID"]));
  }

  /// <summary>
  /// ShowMenuList 显示该资源栏目列表。
  /// </summary>
  /// <param name="ID">该资源栏目的ID号</param>
  ///
  protected void ShowMenuList(int ID)
  {
   string StrMenuFirst;
   StrMenuFirst = "";
   SqlDataAdapter myAdapter = new SqlDataAdapter("Select Source_ParetID,Source_CName From Resources Where Source_ID = " + ID + "",SqlConn);
   DataSet ds = new DataSet();
   myAdapter.Fill(ds,"第一级栏目");

   for (int i = 0; i < ds.Tables["第一级栏目"].Rows.Count; i++)
   {
    StrMenuFirst = "├&nbsp;&nbsp;<a href=__Main.aspx?Source_ID=" + ID + "&Source_ParetID=" + ds.Tables["第一级栏目"].Rows[i].ItemArray[0] + " target=Source_main>" + ds.Tables["第一级栏目"].Rows[i].ItemArray[1] + "</a><br/>";
    
    Label1.Text = StrMenuFirst + ShowChildMenuList(ID,"&#8212;","&nbsp;&nbsp;&nbsp;&nbsp;");
   }
  }

  /// <summary>
  /// ShowMenuList 显示该资源 子栏目列表。
  /// </summary>
  /// <param name="ParentID">父ID</param>
  /// <param name="flag1">前导符号标记</param>
  private string ShowChildMenuList(int ParentID,string flag1,string flag2)
  {
   string StrMenuSecond;
   StrMenuSecond = "";
   SqlDataAdapter myAdapter= new SqlDataAdapter("Select Source_ID,Source_CName From Resources Where Source_ParetID = " + ParentID + "",SqlConn);
   DataSet ds    = new DataSet();
   myAdapter.Fill(ds,"呈现子栏目");
   
   if (ds.Tables["呈现子栏目"].Rows.Count == 0)
   {
    StrMenuSecond   += "";
   }
   else
   {
    for (int ii=0;ii<ds.Tables["呈现子栏目"].Rows.Count;ii++)
    {
     if (ii != ds.Tables["呈现子栏目"].Rows.Count - 1)
     {
      flag2 = flag2 + flag2;
      StrMenuSecond  += flag2 + "├&nbsp;&nbsp;" + flag1 + "<a href=__Main.aspx?Source_ID=" + ParentID + "&Source_ParetID=" + ds.Tables["呈现子栏目"].Rows[ii].ItemArray[0] + " target=Source_main>" + ds.Tables["呈现子栏目"].Rows[ii].ItemArray[1] + "</a><br/>";
      StrMenuSecond  += ShowChildMenuList(Convert.ToInt32(ds.Tables["呈现子栏目"].Rows[ii].ItemArray[0]),flag1,flag2);
     }
     else
     {
      StrMenuSecond  += flag2 + "└&nbsp;&nbsp;" + flag1 + "<a href=__Main.aspx?Source_ID=" + ParentID + "&Source_ParetID=" + ds.Tables["呈现子栏目"].Rows[ii].ItemArray[0] + " target=Source_main>" + ds.Tables["呈现子栏目"].Rows[ii].ItemArray[1] + "</a><br/>";
      StrMenuSecond  += ShowChildMenuList(Convert.ToInt32(ds.Tables["呈现子栏目"].Rows[ii].ItemArray[0]),flag1,flag2);
     }
    }
   }
   return StrMenuSecond;
  }
  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion
 }
}

希望能给大家带来点帮助,在无限级下拉菜单上, 能给点启迪.哈.

posted @ 2005-10-26 18:17    阅读(556)  评论(0编辑  收藏  举报