Исходник от сюда http://www.aspnetmania.com/Code/Code/128.html
- Код: Выделить всё
using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace DN2Portal.WebControlsEx
{
/// <summary>
/// Summary description for GroupDropDownList.
/// </summary>
[ToolboxData("<{0}:GroupDropDownList runat=server></{0}:GroupDropDownList>")]
public class GroupDropDownList : System.Web.UI.WebControls.DropDownList
{
/// <summary>
/// The field in the datasource which provides values for groups
/// </summary>
[DefaultValue(""), Category("Data")]
public virtual string DataGroupField
{
get
{
object obj = this.ViewState["DataGroupField"];
if (obj != null)
{
return (string) obj;
}
return string.Empty;
}
set
{
this.ViewState["DataGroupField"] = value;
}
}
/// <summary>
/// Render this control to the output parameter specified.
/// Based on the source code of the original DropDownList method
/// </summary>
/// <param name="output"> The HTML writer to write out to </param>
protected override void RenderContents(HtmlTextWriter writer)
{
ListItemCollection items = this.Items;
int itemCount = this.Items.Count;
string curGroup = String.Empty;
string itemGroup;
bool bSelected = false;
if (itemCount <= 0)
{
return;
}
for (int i = 0; i < itemCount; i++)
{
ListItem item = items[i];
itemGroup = (string)item.Attributes["DataGroupField"];
if (itemGroup != null && itemGroup != curGroup)
{
if (curGroup != String.Empty)
{
writer.WriteEndTag("optgroup");
writer.WriteLine();
}
curGroup = itemGroup;
writer.WriteBeginTag("optgroup");
writer.WriteAttribute("label", curGroup, true);
writer.Write('>');
writer.WriteLine();
}
writer.WriteBeginTag("option");
if (item.Selected)
{
if (bSelected)
{
throw new HttpException("Cant_Multiselect_In_DropDownList");
}
bSelected = true;
writer.WriteAttribute("selected", "selected", false);
}
writer.WriteAttribute("value", item.Value, true);
writer.Write('>');
HttpUtility.HtmlEncode(item.Text, writer);
writer.WriteEndTag("option");
writer.WriteLine();
}
if (curGroup != String.Empty)
{
writer.WriteEndTag("optgroup");
writer.WriteLine();
}
}
/// <summary>
/// Perform data binding logic that is associated with the control
/// </summary>
/// <param name="e">An EventArgs object that contains the event data</param>
protected override void OnDataBinding(EventArgs e)
{
// Call base method to bind data
base.OnDataBinding(e);
if (this.DataGroupField == String.Empty)
{
return;
}
// For each Item add the attribute "DataGroupField" with value from the datasource
IEnumerable dataSource = GetResolvedDataSource(this.DataSource, this.DataMember);
if (dataSource != null)
{
ListItemCollection items = this.Items;
int i = 0;
string groupField = this.DataGroupField;
foreach (object obj in dataSource)
{
string groupFieldValue = DataBinder.GetPropertyValue(obj, groupField, null);
ListItem item = items[i];
item.Attributes.Add("DataGroupField", groupFieldValue);
i++;
}
}
}
/// <summary>
/// This is copy of the internal ListControl method
/// </summary>
/// <param name="dataSource"></param>
/// <param name="dataMember"></param>
/// <returns></returns>
private IEnumerable GetResolvedDataSource(object dataSource, string dataMember)
{
if (dataSource != null)
{
IListSource source1 = dataSource as IListSource;
if (source1 != null)
{
IList list1 = source1.GetList();
if (!source1.ContainsListCollection)
{
return list1;
}
if ((list1 != null) && (list1 is ITypedList))
{
ITypedList list2 = (ITypedList) list1;
PropertyDescriptorCollection collection1 = list2.GetItemProperties(new PropertyDescriptor[0]);
if ((collection1 == null) || (collection1.Count == 0))
{
throw new HttpException("ListSource_Without_DataMembers");
}
PropertyDescriptor descriptor1 = null;
if ((dataMember == null) || (dataMember.Length == 0))
{
descriptor1 = collection1[0];
}
else
{
descriptor1 = collection1.Find(dataMember, true);
}
if (descriptor1 != null)
{
object obj1 = list1[0];
object obj2 = descriptor1.GetValue(obj1);
if ((obj2 != null) && (obj2 is IEnumerable))
{
return (IEnumerable) obj2;
}
}
throw new HttpException("ListSource_Missing_DataMember");
}
}
if (dataSource is IEnumerable)
{
return (IEnumerable) dataSource;
}
}
return null;
}
}
}
То что перевел:
- Код: Выделить всё
Imports System
Imports System.Collections
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.ComponentModel
<ToolboxData("<{0}:GroupDropDownList runat=server></{0}:GroupDropDownList>")> Public Class DropDownListEx : Inherits DropDownList
<DefaultValue(""), Category("Data")> Property DataGroupField() As String
Get
Dim _Obj As Object = Me.ViewState("DataGroupField")
If Not _Obj Is Nothing Then Return CType(_Obj, String)
Return String.Empty
End Get
Set(ByVal value As String)
Me.ViewState("DataGroupField") = value
End Set
End Property
Protected Overrides Sub RenderContents(ByVal writer As System.Web.UI.HtmlTextWriter)
Dim _ItemsCount As Integer = Me.Items.Count
Dim _CurGroup As String = String.Empty
Dim _ItemGroup As String = ""
Dim _Selected As Boolean = False
If _ItemsCount < 1 Then Return
For Each _Item As ListItem In Me.Items
_ItemGroup = CType(_Item.Attributes("DataGroupField"), String)
If Not _ItemGroup Is Nothing And _ItemGroup <> _CurGroup Then
If _CurGroup <> String.Empty Then
writer.WriteEndTag("optgroup")
writer.WriteLine()
End If
_CurGroup = _ItemGroup
writer.WriteBeginTag("optgroup")
writer.WriteAttribute("label", _CurGroup, True)
writer.Write(">")
writer.WriteLine()
End If
writer.WriteBeginTag("option")
If _Item.Selected Then
If _Selected Then Throw New HttpException("Cant_Multiselect_In_DropDownList")
_Selected = True
writer.WriteAttribute("selected", "selected", False)
End If
writer.WriteAttribute("value", _Item.Value, True)
writer.Write(">")
HttpUtility.HtmlEncode(_Item.Text, writer)
writer.WriteEndTag("option")
writer.WriteLine()
Next
If _CurGroup <> String.Empty Then
writer.WriteEndTag("optgroup")
writer.WriteLine()
End If
End Sub
End Class