項(xiàng)目結(jié)構(gòu)圖如下所示:
Controls類(lèi)庫(kù)SmartDropDownList.cs代碼如下所示:
SmartDropDownList.cs
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>
/// 構(gòu)造函數(shù)
/// </summary>
public SmartDropDownList() { }
/// <summary>
/// 將控件的內(nèi)容呈現(xiàn)到指定的編寫(xiě)器中
/// </summary>
/// <param name="writer"></param>
protected override void RenderContents(HtmlTextWriter writer)
{
OptionGroupRenderContents(writer);
}
/// <summary>
/// 呈現(xiàn)Option或OptionGroup
/// </summary>
/// <param name="writer">writer</param>
private void OptionGroupRenderContents(HtmlTextWriter writer)
{
// 是否需要呈現(xiàn)OptionGroup的EndTag
bool writerEndTag = false;
foreach (ListItem li in this.Items)
{
// 如果沒(méi)有optgroup屬性則呈現(xiàn)Option
if (li.Value != this.OptionGroupValue)
{
// 呈現(xiàn)Option
RenderListItem(li, writer);
}
// 如果有optgroup屬性則呈現(xiàn)OptionGroup
else
{
if (writerEndTag)
// 呈現(xiàn)OptionGroup的EndTag
OptionGroupEndTag(writer);
else
writerEndTag = true;
// 呈現(xiàn)OptionGroup的BeginTag
OptionGroupBeginTag(li, writer);
}
}
if (writerEndTag)
// 呈現(xiàn)OptionGroup的EndTag
OptionGroupEndTag(writer);
}
/// <summary>
/// 呈現(xiàn)OptionGroup的BeginTag
/// </summary>
/// <param name="li">OptionGroup數(shù)據(jù)項(xiàng)</param>
/// <param name="writer">writer</param>
private void OptionGroupBeginTag(ListItem li, HtmlTextWriter writer)
{
writer.WriteBeginTag("optgroup");
// 寫(xiě)入OptionGroup的label
writer.WriteAttribute("label", li.Text);
foreach (string key in li.Attributes.Keys)
{
// 寫(xiě)入OptionGroup的其它屬性
writer.WriteAttribute(key, li.Attributes[key]);
}
writer.Write(HtmlTextWriter.TagRightChar);
writer.WriteLine();
}
/// <summary>
/// 呈現(xiàn)OptionGroup的EndTag
/// </summary>
/// <param name="writer">writer</param>
private void OptionGroupEndTag(HtmlTextWriter writer)
{
writer.WriteEndTag("optgroup");
writer.WriteLine();
}
/// <summary>
/// 呈現(xiàn)Option
/// </summary>
/// <param name="li">Option數(shù)據(jù)項(xiàng)</param>
/// <param name="writer">writer</param>
private void RenderListItem(ListItem li, HtmlTextWriter writer)
{
writer.WriteBeginTag("option");
// 寫(xiě)入Option的Value
writer.WriteAttribute("value", li.Value, true);
if (li.Selected)
{
// 如果該Option被選中則寫(xiě)入selected
writer.WriteAttribute("selected", "selected", false);
}
foreach (string key in li.Attributes.Keys)
{
// 寫(xiě)入Option的其它屬性
writer.WriteAttribute(key, li.Attributes[key]);
}
writer.Write(HtmlTextWriter.TagRightChar);
// 寫(xiě)入Option的Text
HttpUtility.HtmlEncode(li.Text, writer);
writer.WriteEndTag("option");
writer.WriteLine();
}
/// <summary>
/// 用于添加SmartDropDownList的分組項(xiàng)的ListItem的Value值
/// </summary>
[
Browsable(true),
Description("用于添加DropDownList的分組項(xiàng)的ListItem的Value值"),
Category("擴(kuò)展")
]
public virtual string OptionGroupValue
{
get
{
string s = (string)ViewState["OptionGroupValue"];
return (s == null) ? "optgroup" : s;
}
set
{
ViewState["OptionGroupValue"] = value;
}
}
}
}
smartDropDownList.aspx頁(yè)面代碼如下所示:
smartDropDownList.aspx
代碼如下:
<%@ 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">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<cc1:SmartDropDownList ID="SmartDropDownList1" runat="server">
</cc1:SmartDropDownList>
</div>
</form>
</body>
</html>
smartDropDownList.aspx頁(yè)面smartDropDownList.cs如下所示:
smartDropDownList.cs
代碼如下:
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>
/// 板塊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("這里是板塊"+i,i.ToString());
list.Add(li);
}
return list;
}
/// <summary>
/// 子板塊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("這一項(xiàng)是上一級(jí)的子板塊", "這里是你像傳遞參數(shù)的值");
list_sub.Add(li_sub);
}
return list_sub;
}
/// <summary>
/// 綁定下拉控件數(shù)據(jù)
/// </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();
}
}
你可以根據(jù)你數(shù)據(jù)庫(kù)的設(shè)計(jì)來(lái)綁定值,這里沒(méi)用數(shù)據(jù)庫(kù),只是說(shuō)明思路!
源碼下載,點(diǎn)擊這里!
版權(quán)所有,轉(zhuǎn)載請(qǐng)注明出處!聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com