無限級下拉式清單的無限級分級[asp.net])

來源:互聯網
上載者:User
來源:http://hi.baidu.com/kb81/blog/item/c8315f8236e85aa 

近期做的一個網站需要一個無限級下拉式清單的無限級分級功能,於是就有了下面的小作品。

       開始以為無限級下拉式清單功能應該很簡單,無非就是用一個遞迴演算法就好啦, 程式其實就是一個遞迴演算法。

 private void GetArticleCategory(string pid)

    {

        SqlConnection conn = new SqlConnection("server=.;database=test;uid=sa;pwd=;");

        string sql = "select Articlesgroup_id,Groupname from Articlesgroup where Articlesgroup_parent_id=@pid order by Groupname";

        SqlCommand cmd = new SqlCommand(sql, conn);

        SqlParameter Pid = new SqlParameter("@pid", SqlDbType.Int);

        Pid.Value = pid;

        cmd.Parameters.Add(Pid);

        conn.Open();

        SqlDataReader sdr = cmd.ExecuteReader();

        while (sdr.Read())

        { 

            this.DropDownList1.Items.Add(new ListItem(toadd + " " + sdr[1].ToString(), sdr[0].ToString()));

            toadd += "─┴";

            this.GetArticleCategory(sdr[0].ToString());

           toadd = toadd.Substring(0, toadd.Length - 2);  //開始這裡沒有處理好,層次關係顯示不出來  :)

        }

        sdr.Close();

        conn.Close();

    }

調用方法:在Page_Load裡調用

  protected void Page_Load(object sender, EventArgs e)

    {

        this.keyword.Attributes.Add("onfocus","if( this.value=='key key key') {this.value='' };");

        if (!Page.IsPostBack)

        {

            this.GetArticleCategory("0");

        }

        this.DropDownList1.Items.Insert(0, new ListItem("Search All", "all"));

    }

 在類裡邊還要定義一個變數

private string toadd = "".

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.