Asp.net DropDownList custom control to make your classification clearer

Source: Internet
Author: User

We can see that Discuz is merged by two drop-down lists. I found some information on the Internet and wrote this small source code. I will share it with you here!
Run the command, as shown in the following figure (the deep black area is not selected, because it is a classification of the upper level ):

The project structure is as follows:

The code of the Controls class library SmartDropDownList. cs is as follows:

SmartDropDownList. cs
Copy codeThe Code is 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>
/// Constructor
/// </Summary>
Public SmartDropDownList (){}
/// <Summary>
/// Present the control content to the specified writer
/// </Summary>
/// <Param name = "writer"> </param>
Protected override void RenderContents (HtmlTextWriter writer)
{
OptionGroupRenderContents (writer );
}
/// <Summary>
/// Present Option or OptionGroup
/// </Summary>
/// <Param name = "writer"> writer </param>
Private void OptionGroupRenderContents (HtmlTextWriter writer)
{
// Whether to display the EndTag of OptionGroup
Bool writerEndTag = false;
Foreach (ListItem li in this. Items)
{
// If the optgroup attribute is not present, the Option is displayed.
If (li. Value! = This. OptionGroupValue)
{
// Rendering Option
RenderListItem (li, writer );
}
// If the optgroup attribute exists, OptionGroup is displayed.
Else
{
If (writerEndTag)
// Present the EndTag of OptionGroup
OptionGroupEndTag (writer );
Else
WriterEndTag = true;
// Present the optiintag of OptionGroup
OptionGroupBeginTag (li, writer );
}
}
If (writerEndTag)
// Present the EndTag of OptionGroup
OptionGroupEndTag (writer );
}
/// <Summary>
/// Present the optiintag of OptionGroup
/// </Summary>
/// <Param name = "li"> OptionGroup data item </param>
/// <Param name = "writer"> writer </param>
Private void OptionGroupBeginTag (ListItem li, HtmlTextWriter writer)
{
Writer. WriteBeginTag ("optgroup ");
// Write the OptionGroup label
Writer. WriteAttribute ("label", li. Text );
Foreach (string key in li. Attributes. Keys)
{
// Write other OptionGroup attributes
Writer. WriteAttribute (key, li. Attributes [key]);
}
Writer. Write (HtmlTextWriter. TagRightChar );
Writer. WriteLine ();
}
/// <Summary>
/// Present the EndTag of OptionGroup
/// </Summary>
/// <Param name = "writer"> writer </param>
Private void OptionGroupEndTag (HtmlTextWriter writer)
{
Writer. WriteEndTag ("optgroup ");
Writer. WriteLine ();
}
/// <Summary>
/// Rendering 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 the Option Value
Writer. WriteAttribute ("value", li. Value, true );
If (li. Selected)
{
// If this Option is selected, it is written to selected.
Writer. WriteAttribute ("selected", "selected", false );
}
Foreach (string key in li. Attributes. Keys)
{
// Write other attributes of Option
Writer. WriteAttribute (key, li. Attributes [key]);
}
Writer. Write (HtmlTextWriter. TagRightChar );
// Write the Text of Option
HttpUtility. HtmlEncode (li. Text, writer );
Writer. WriteEndTag ("option ");
Writer. WriteLine ();
}
/// <Summary>
/// Value of the ListItem used to add the group item of SmartDropDownList
/// </Summary>
[
Browsable (true ),
Description ("Value of ListItem used to add the group items of DropDownList "),
Category ("extension ")
]
Public virtual string OptionGroupValue
{
Get
{
String s = (string) ViewState ["OptionGroupValue"];
Return (s = null )? "Optgroup": s;
}
Set
{
ViewState ["OptionGroupValue"] = value;
}
}
}
}

The code of the smartDropDownList. aspx page is as follows:
SmartDropDownList. aspx
Copy codeThe Code is as follows:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "smartDropDownList. aspx. cs" Inherits = "smartDropDownList" %>
<% @ Register Assembly = "Controls" Namespace = "mydream. Controls" TagPrefix = "PC3" %>
<! 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>
<PC3: SmartDropDownList ID = "SmartDropDownList1" runat = "server">
</PC3: SmartDropDownList>
</Div>
</Form>
</Body>
</Html>

SmartDropDownList. aspx page smartDropDownList. cs is as follows:
SmartDropDownList. cs
Copy codeThe Code is 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 section" + I, I. ToString ());
List. Add (li );
}
Return list;
}
/// <Summary>
/// Submodule 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 item is a subsection of the upper level", "Here is the value of the parameter you want to pass ");
List_sub.Add (li_sub );
}
Return list_sub;
}
/// <Summary>
/// Bind the drop-down 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 bind a value based on your database design. There is no database here, just to illustrate the concept!
Download the source code. Click here!
Copyright. For more information, see the source!

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.