Asp.net dropdownlist three-level linkage drop-down menu implementation code

Source: Internet
Author: User

CopyCode The Code is as follows: if (! Ispostback)
{
// List of level-1 Categories
This. dropdownlist1.datasource = DSBB. selectsubjct1 ();
This. dropdownlist1.datatextfield = "cname ";
This. dropdownlist1.datavaluefield = "cCode ";
This. dropdownlist1.databind ();
This. dropdownlist1.items. insert (0, new listitem ("select a level-1 category", "0 "));
This. dropdownlist8.items. insert (0, new listitem ("select Level 2 Classification", "0 "));
This. dropdownlist9.items. insert (0, new listitem ("select Level 3 Classification", "0 "));
// List of secondary categories

}
/// <Summary>
/// Bind the second-level category
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Protected void dropdownlistincluselectedindexchanged (Object sender, eventargs E)
{
Libs. database. dbbase DBB = new libs. database. dbbase ();
If (convert. toint32 (this. dropdownlist1.selectedvalue) = 0) // clear the list content
{
This. dropdownlist8.items. Clear ();
This. dropdownlist8.items. insert (0, new listitem ("select Level 2 Classification", "0 "));
This. dropdownlist9.items. Clear ();
This. dropdownlist9.items. insert (0, new listitem ("select Level 3 Classification", "0 "));
}
Else // list of secondary categories
{
This. dropdownlist8.datasource = DBB. selectsubjct2 (this. dropdownlist1.selectedvalue. substring (0, 2 ));
This. dropdownlist8.datatextfield = "cname ";
This. dropdownlist8.datavaluefield = "cCode ";
This. dropdownlist8.databind ();
This. dropdownlist8.items. insert (0, new listitem ("select Level 2 Classification", "0 "));
This. dropdownlist9.items. Clear (); // clear the third category
This. dropdownlist9.items. insert (0, new listitem ("select Level 3 Classification", "0 "));
}
}
/// <Summary>
/// Bind a third-level category
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Protected void dropdownlist8_selectedindexchanged (Object sender, eventargs E)
{
Libs. database. dbbase DBASE = new libs. database. dbbase ();
This. dropdownlist9.datasource = DBASE. selectsubject3 (this. dropdownlist8.selectedvalue. substring (0, 4 ));
This. dropdownlist9.datatextfield = "cname ";
This. dropdownlist9.datavaluefield = "cCode ";
This. dropdownlist9.databind ();
This. dropdownlist9.items. insert (0, new listitem ("select Level 3 Classification", "0 "));
}

Dbbase. CS page: Copy code The Code is as follows: // <summary>
/// Query the first-level column
/// </Summary>
/// <Returns> </returns>
Public dataset selectsubjct1 ()
{
String con = system. configuration. configurationsettings. appsettings ["sqlconn"];
Sqlconnection conn = new sqlconnection (CON );
String sqlstr = "select kndid, cCode, cname, clevel from kind where clevel = 1 ";
Dataset DST = new dataset ();
Sqldataadapter SDA = new sqldataadapter (sqlstr, Conn );
Try
{
SDA. Fill (DST );
Return DST;
}
Catch (exception ex)
{
Throw new exception (ex. Message );
}
Finally
{
Conn. Close ();
}
}
/// <Summary>
/// Query the content of the second-level column
/// </Summary>
/// <Param name = "cCode"> </param>
/// <Returns> </returns>
Public dataset selectsubjct2 (string cCode)
{
String conn1 = system. configuration. configurationsettings. deleettings ["sqlconn"];
Sqlconnection conn = new sqlconnection (conn1 );
String sqqq = "select kndid, cCode, cname, clevel from kind where clevel = 2 and cCode like '" + cCode + "% '";
Dataset DSS = new dataset ();
Sqldataadapter SDD = new sqldataadapter (sqqq, Conn );
Try
{
Sdd. Fill (DSS );
Return DSS;
}
Catch (exception ex)
{
Throw new exception (ex. Message );
}
Finally
{
Conn. Close ();
}
}
/// <Summary>
/// Query the content of a third-level column
/// </Summary>
/// <Param name = "CDE"> </param>
/// <Returns> </returns>
Public dataset selectsubject3 (string CDE)
{
String conn2 = system. configuration. configurationsettings. appsettings ["sqlconn"];
Sqlconnection conn = new sqlconnection (conn2 );
String sqq = "select kndid, cCode, cname, clevel from kind where clevel = 3 and cCode like '" + CDE + "% '";
Dataset DST = new dataset ();
Sqldataadapter sdaa = new sqldataadapter (sqq, Conn );
Try
{
Sdaa. Fill (DST );
Return DST;
}
Catch (exception ex)
{
Throw new exception (ex. Message );
}
Finally
{
Conn. Close ();
}
}

Note: dropdownlist1_selectedindexchanged event, autopostback = "true"

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.