Looking for a long time on the internet did not find an example, no way, want to be lazy also stolen, or their own honest to write a bar. No more nonsense, below please see the detailed method I do.
1. Infinite classification must be database support, users can dynamically add their classification, the following is the structure of the database:
The database has three main fields, which is the most basic. The first is the primary key, the second is the name of the category, and the third is the parent directory ID number.
2. The result of the finished product: (is the most concerned about this ah?) See what it looks like and see how the program is.
See!! It looks like this, and this level of relationship is the result of the data shown above. What do you think? It's OK ...
3. The last is to provide the program, the program is actually a recursive algorithm.
Copy Code code as follows:
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); At first there is no processing, the hierarchical relationship does not show up:
}
Sdr. Close ();
Conn. Close ();
}
Call method: called in Page_Load
Copy Code code as follows:
protected void Page_Load (object sender, EventArgs e)
{
THIS.KEYWORD.ATTRIBUTES.ADD ("onfocus", "if (this.value== ' key key ') {this.value= '};");
if (! Page.IsPostBack)
{
This. Getarticlecategory ("0");
}
This. DropDownList1.Items.Insert (0, New ListItem ("Search All", "all"));
}
In the class, you also define a variable
Copy Code code as follows:
private string toadd = "├";