See Discuz is 2 drop-down list to merge, the Internet to find some information, and then wrote this small source, here and you share!
Run the effect chart as shown below (dark places are not selected because that is the classification at the top level):
The project structure diagram looks like this:
The Controls class library SmartDropDownList.cs code looks like this:
SmartDropDownList.cs
Copy Code code as follows:
Using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Web.UI.WebControls;
6 using System.Web.UI;
7 using System.ComponentModel;
8 using System.Web;
9
Namespace Mydream. Controls
{
[ToolBoxData ("<{0}:smartdropdownlist runat=server></{0}:smartdropdownlist>")]
public class Smartdropdownlist:dropdownlist
{
<summary>
Constructors
</summary>
Public smartdropdownlist () {}
<summary>
Renders the contents of the control to the specified writer
</summary>
<param name= "Writer" ></param>
protected override void RenderContents (HtmlTextWriter writer)
{
Optiongrouprendercontents (writer);
}
<summary>
Render option or OptionGroup
</summary>
<param name= "Writer" >writer</param>
private void Optiongrouprendercontents (HtmlTextWriter writer)
{
Do you need to present the OptionGroup Endtag
BOOL Writerendtag = false;
foreach (ListItem li in this.) Items)
{
Render option if there is no Optgroup property
if (li. Value!= this. Optiongroupvalue)
{
Render option
Renderlistitem (Li, writer);
}
Renders OptionGroup if there is a Optgroup property
Else
{
if (Writerendtag)
Present the OptionGroup Endtag
Optiongroupendtag (writer);
Else
Writerendtag = true;
Present the OptionGroup Begintag
Optiongroupbegintag (Li, writer);
}
}
if (Writerendtag)
Present the OptionGroup Endtag
Optiongroupendtag (writer);
}
<summary>
Present the OptionGroup Begintag
</summary>
<param name= "Li" >optiongroup data item </param>
<param name= "Writer" >writer</param>
private void Optiongroupbegintag (ListItem Li, HtmlTextWriter writer)
{
Writer. WriteBeginTag ("Optgroup");
Write label to OptionGroup
Writer. WriteAttribute ("label", Li. Text);
foreach (string key in Li. Attributes.keys)
{
Other properties written to OptionGroup
Writer. WriteAttribute (Key, Li. Attributes[key]);
}
Writer. Write (Htmltextwriter.tagrightchar);
Writer. WriteLine ();
}
<summary>
Present the OptionGroup Endtag
</summary>
<param name= "Writer" >writer</param>
private void Optiongroupendtag (HtmlTextWriter writer)
{
Writer. Writeendtag ("Optgroup");
Writer. WriteLine ();
}
<summary>
Render option
</summary>
<param name= "Li" >option data item </param>
<param name= "Writer" >writer</param>
private void Renderlistitem (ListItem Li, HtmlTextWriter writer)
{
Writer. WriteBeginTag ("option");
Write to option value
Writer. WriteAttribute ("value", Li. Value, True);
if (li. Selected)
{
Write selected if the option is selected
Writer. WriteAttribute ("Selected", "selected", false);
}
foreach (string key in Li. Attributes.keys)
{
Write additional properties for option
Writer. WriteAttribute (Key, Li. Attributes[key]);
}
Writer. Write (Htmltextwriter.tagrightchar);
Writes option's text
Httputility.htmlencode (li. Text, writer);
Writer. Writeendtag ("option");
Writer. WriteLine ();
}
<summary>
Value value of ListItem for the grouping item used to add Smartdropdownlist
</summary>
[
Browsable (True),
Description ("Value value for the ListItem of the group item used to add DropDownList"),
Category ("extended")
]
Public virtual String Optiongroupvalue
{
Get
{
string s = (string) viewstate["Optiongroupvalue"];
return (s = = null)? "Optgroup": s;
}
Set
{
viewstate["Optiongroupvalue"] = value;
}
}
}
}
The smartdropdownlist.aspx page code looks like this:
Smartdropdownlist.aspx
Copy Code code as follows:
<%@ Page language= "C #" autoeventwireup= "true" codefile= "SmartDropDownList.aspx.cs" inherits= "smartdropdownlist"% >
<%@ Register assembly= "Controls" namespace= "Mydream". Controls "tagprefix=" CC1 "%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title></title>
<body>
<form id= "Form1" runat= "Server" >
<div>
<cc1:smartdropdownlist id= "SmartDropDownList1" runat= "Server" >
</cc1:SmartDropDownList>
</div>
</form>
</body>
The smartdropdownlist.aspx page SmartDropDownList.cs looks like this:
SmartDropDownList.cs
Copy Code code as follows:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Data;
public partial class SmartDropDownList:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
if (! IsPostBack)
{
This. Bindddlist (this. SMARTDROPDOWNLIST1);
}
}
<summary>
Plate ListItem
</summary>
<returns></returns>
Private list<listitem> getboardlist ()
{
list<listitem> list = new list<listitem> ();
for (int i = 1; i < 6; i++)
{
ListItem li = new ListItem ("Here is the plate" +i,i.tostring ());
List. Add (LI);
}
return list;
}
<summary>
Sub-plate ListItem
</summary>
<returns></returns>
Private list<listitem> getsubboardlist ()
{
List<listitem> List=this. Getboardlist ();
list<listitem> list_sub = new list<listitem> ();
foreach (ListItem li in list)
{
ListItem li_sub = new ListItem ("This is the upper level of the sub plate", "Here is what you like to pass the parameter value");
List_sub. ADD (li_sub);
}
return list_sub;
}
<summary>
Bind pull Control Data
</summary>
private void Bindddlist (Mydream. Controls.smartdropdownlist smartddlist)
{
Smartddlist. Items.clear ();
list<listitem> list = this. Getboardlist ();
foreach (ListItem li in list)
{
ListItem item = new ListItem ("--" + li. Text, "Optgroup");
Smartddlist. Items.Add (item);
List<listitem> list_sub = this. Getsubboardlist ();
foreach (ListItem li_sub in List_sub)
{
Smartddlist. Items.Add (li_sub);
}
}
Smartddlist. DataBind ();
}
}
You can be based on the design of your database to bind the value, there is no database, just explain the idea!
Download the source, click here!
All rights reserved, reprint please specify the source!