using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Web;
namespace YYControls
![](http://static.javashuo.com/static/loading.gif)
{
/// <summary>
/// SmartDropDownList類的屬性部分
/// </summary>
public partial
class SmartDropDownList
![](http://static.javashuo.com/static/loading.gif)
{
/// <summary>
/// 呈現Option或OptionGroup
/// </summary>
/// <param name="writer">writer</param>
private
void OptionGroupRenderContents(HtmlTextWriter writer)
![](http://static.javashuo.com/static/loading.gif)
{
// 是否須要呈現OptionGroup的EndTag
bool writerEndTag =
false;
foreach (ListItem li
in
this.Items)
![](http://static.javashuo.com/static/loading.gif)
{
// 若是沒有optgroup屬性則呈現Option
if (li.Value !=
this.OptionGroupValue)
![](http://static.javashuo.com/static/loading.gif)
{
// 呈現Option
![](http://static.javashuo.com/static/loading.gif)
RenderListItem(li, writer);
![](http://static.javashuo.com/static/loading.gif)
}
// 若是有optgroup屬性則呈現OptionGroup
else
![](http://static.javashuo.com/static/loading.gif)
{
if (writerEndTag)
// 呈現OptionGroup的EndTag
![](http://static.javashuo.com/static/loading.gif)
OptionGroupEndTag(writer);
else
![](http://static.javashuo.com/static/loading.gif)
writerEndTag =
true;
// 呈現OptionGroup的BeginTag
![](http://static.javashuo.com/static/loading.gif)
OptionGroupBeginTag(li, writer);
![](http://static.javashuo.com/static/loading.gif)
}
![](http://static.javashuo.com/static/loading.gif)
}
if (writerEndTag)
// 呈現OptionGroup的EndTag
![](http://static.javashuo.com/static/loading.gif)
OptionGroupEndTag(writer);
![](http://static.javashuo.com/static/loading.gif)
}
/// <summary>
/// 呈現OptionGroup的BeginTag
/// </summary>
/// <param name="li">OptionGroup數據項</param>
/// <param name="writer">writer</param>
private
void OptionGroupBeginTag(ListItem li, HtmlTextWriter writer)
![](http://static.javashuo.com/static/loading.gif)
{
![](http://static.javashuo.com/static/loading.gif)
writer.WriteBeginTag(
"optgroup");
// 寫入OptionGroup的label
![](http://static.javashuo.com/static/loading.gif)
writer.WriteAttribute(
"label", li.Text);
foreach (
string key
in li.Attributes.Keys)
![](http://static.javashuo.com/static/loading.gif)
{
// 寫入OptionGroup的其它屬性
![](http://static.javashuo.com/static/loading.gif)
writer.WriteAttribute(key, li.Attributes[key]);
![](http://static.javashuo.com/static/loading.gif)
}
![](http://static.javashuo.com/static/loading.gif)
writer.Write(HtmlTextWriter.TagRightChar);
![](http://static.javashuo.com/static/loading.gif)
writer.WriteLine();
![](http://static.javashuo.com/static/loading.gif)
}
/// <summary>
/// 呈現OptionGroup的EndTag
/// </summary>
/// <param name="writer">writer</param>
private
void OptionGroupEndTag(HtmlTextWriter writer)
![](http://static.javashuo.com/static/loading.gif)
{
![](http://static.javashuo.com/static/loading.gif)
writer.WriteEndTag(
"optgroup");
![](http://static.javashuo.com/static/loading.gif)
writer.WriteLine();
![](http://static.javashuo.com/static/loading.gif)
}
/// <summary>
/// 呈現Option
/// </summary>
/// <param name="li">Option數據項</param>
/// <param name="writer">writer</param>
private
void RenderListItem(ListItem li, HtmlTextWriter writer)
![](http://static.javashuo.com/static/loading.gif)
{
![](http://static.javashuo.com/static/loading.gif)
writer.WriteBeginTag(
"option");
// 寫入Option的Value
![](http://static.javashuo.com/static/loading.gif)
writer.WriteAttribute(
"value", li.Value,
true);
if (li.Selected)
![](http://static.javashuo.com/static/loading.gif)
{
// 若是該Option被選中則寫入selected
![](http://static.javashuo.com/static/loading.gif)
writer.WriteAttribute(
"selected",
"selected",
false);
![](http://static.javashuo.com/static/loading.gif)
}
foreach (
string key
in li.Attributes.Keys)
![](http://static.javashuo.com/static/loading.gif)
{
// 寫入Option的其它屬性
![](http://static.javashuo.com/static/loading.gif)
writer.WriteAttribute(key, li.Attributes[key]);
![](http://static.javashuo.com/static/loading.gif)
}
![](http://static.javashuo.com/static/loading.gif)
writer.Write(HtmlTextWriter.TagRightChar);
// 寫入Option的Text
![](http://static.javashuo.com/static/loading.gif)
HttpUtility.HtmlEncode(li.Text, writer);
![](http://static.javashuo.com/static/loading.gif)
writer.WriteEndTag(
"option");
![](http://static.javashuo.com/static/loading.gif)
writer.WriteLine();
![](http://static.javashuo.com/static/loading.gif)
}
![](http://static.javashuo.com/static/loading.gif)
}
![](http://static.javashuo.com/static/loading.gif)
}