Use dropdownlist in Asp.net to implement an infinitely classified menu

Source: Internet
Author: User

As required by the project, create a menu of unlimited classification, and finally select to use the droopdownlist control. The time is urgent and only postCodeIf you have the same requirements, you can see
(1) database table description:
Pkid: Needless to say
Classname: category name
Upid: ID of the upper-level category (0 by default, indicating the first-level category)

(2) The Code is as follows:
<1> the default. aspx Page code is as follows:

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: placeholder id = "PH" runat = "server"> </ASP: placeholder>
<Asp: button id = "btn_enter" runat = "server" onclick = "btn_enter_click" text = "OK"/> </div>
</Form>
</Body>
</Html>

<2> the default. aspx. CS code is as follows:

Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. collections;
Using system. Data. sqlclient;

Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
Arraylist al;
If (session ["controls"]! = NULL)
{

Al = (arraylist) session ["controls"];
Ph. Controls. Clear ();
For (INT I = 0; I <Al. Count; I ++)
{
Dropdownlist DDL = (dropdownlist) al [I];
DDL. selectedindexchanged + = new eventhandler (dl_selectedindexchanged );
Ph. Controls. Add (DDL );
}
}

If (! Ispostback)
{
Createcontrols ("select * From t_sj_classinfo where upid = 0", 0 );
}
}
Void createcontrols (string SQL, int curid)
{
Arraylist al;

If (session ["controls"] = NULL)
Al = new arraylist ();
Else
{
Al = (arraylist) session ["controls"];
For (INT I = 0; I <Al. Count; I ++)
{
Dropdownlist ddl_remove = (dropdownlist) al [I];

If (convert. toint32 (ddl_remove.id)> curid)
{
I --;
Al. Remove (ddl_remove );
Ph. Controls. Remove (ddl_remove );
}
}
}

 

Sqlconnection conn = new sqlconnection ("Server = localhost; uid = sa; Pwd = 123456; database = test ");

Sqlcommand cmd = new sqlcommand (SQL, Conn );
Datatable dt = new datatable ();
Sqldataadapter DRP = new sqldataadapter (CMD );
DRP. Fill (DT );
If (Dt. Rows. Count> 0)
{
Dropdownlist DL = new dropdownlist ();
Curid ++;
DL. ID = curid. tostring ();
DL. autopostback = true;
DL. selectedindexchanged + = new eventhandler (dl_selectedindexchanged );
DL. datasource = DT;
DL. datatextfield = "classname ";
DL. datavaluefield = "pkid ";
DL. databind ();
DL. Items. insert (0, new listitem ("-- select --", "0 "));
Al. Add (DL );
Ph. Controls. Add (DL );
Session ["controls"] = Al;
}

}
Void dl_selectedindexchanged (Object sender, eventargs E)
{
Dropdownlist DL = (dropdownlist) sender;
String LBID = DL. selectedvalue;
Createcontrols ("select * From t_sj_classinfo where upid =" + LBID, convert. toint32 (DL. ID ));
}

Protected void btn_enter_click (Object sender, eventargs E)
{
Controlcollection CTL = Ph. controls;
System. Text. stringbuilder sb = new system. Text. stringbuilder ();

Foreach (control C in CTL)
{
// If there are other server controls in pH, the server control type must be added here for determination.
Dropdownlist DL = (dropdownlist) C;
SB. append ("selected text:" + DL. selecteditem. Text + "selected value:" + DL. selecteditem. Value + "<br/> ");
}
Response. Write (sb. tostring ());
}
}

The following figure shows the result page:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.