[Reprinted] Asp.net dropdownlist infinitely graded

Source: Internet
Author: User

Http://hi.baidu.com/kb81/blog/item/c8315f8236e85aa20cf4d201.html

Recently, a website requires an infinitely hierarchical drop-down list, so the following small works are available.
At first, I thought that the unlimited drop-down list function should be very simple. It is nothing more than a recursion.AlgorithmThat's fine, but there will be some vague things when doing it. I haven't found an example on the Internet for a long time. I can't help it. I can't steal it if I want to be lazy. I 'd like to write it honestly. Not much nonsense. Please refer to my detailed method below.

1. The database must support unlimited classification. You can dynamically add their classification. below is the structure of the database:

The database mainly has three fields, which is the most basic. The first is the primary key, the second is the category name, and the third is the parent directory ID.

2. Finished Products: (do you most care about this? Let's see what it looks like.ProgramWhy)

Look !! This is what it looks like. This hierarchy is the result of the above data. How is it? Okay...

3. Finally, it is the provider. The program is actually a recursive algorithm.
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 + = "-Example ";
This. getarticlecategory (SDR [0]. tostring ());
Toadd = toadd. substring (0, toadd. Length-2); // It is not properly processed at the beginning, and the hierarchy cannot be displayed :)
}
SDR. Close ();
Conn. Close ();
}

Call method: Call in 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 "));
}

Define a variable in the class.
Private string toadd = "external ";

everything is done here. That's easy .....

Related Article

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.