猿问

使用<optgroup>的asp.net(webforms)的Dropdownlist控件?

谁能为asp.net(3.5)推荐一个可以呈现选项组的下拉列表控件?谢谢



Cats萌萌
浏览 455回答 3
3回答

函数式编程

我过去使用过标准控件,只是为其添加了一个简单的ControlAdapter,它将覆盖默认行为,因此它可以在某些位置呈现<optgroup>。即使您的控件不需要特殊的行为,这也能很好地工作,因为附加功能不会妨碍您。请注意,这是出于特定目的并用.Net 2.0编写,因此它可能也不适合您,但至少应为您提供一个起点。另外,您还必须在项目中使用.browserfile进行连接(示例请参见文章末尾)。'This codes makes the dropdownlist control recognize items with "--"'for the label or items with an OptionGroup attribute and render them'as <optgroup> instead of <option>.Public Class DropDownListAdapter&nbsp; &nbsp; Inherits System.Web.UI.WebControls.Adapters.WebControlAdapter&nbsp; &nbsp; Protected Overrides Sub RenderContents(ByVal writer As HtmlTextWriter)&nbsp; &nbsp; &nbsp; &nbsp; Dim list As DropDownList = Me.Control&nbsp; &nbsp; &nbsp; &nbsp; Dim currentOptionGroup As String&nbsp; &nbsp; &nbsp; &nbsp; Dim renderedOptionGroups As New Generic.List(Of String)&nbsp; &nbsp; &nbsp; &nbsp; For Each item As ListItem In list.Items&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Page.ClientScript.RegisterForEventValidation(list.UniqueID, item.Value)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If item.Attributes("OptionGroup") IsNot Nothing Then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'The item is part of an option group&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentOptionGroup = item.Attributes("OptionGroup")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If Not renderedOptionGroups.Contains(currentOptionGroup) Then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'the header was not written- do that first&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'TODO: make this stack-based, so the same option group can be used more than once in longer select element (check the most-recent stack item instead of anything in the list)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If (renderedOptionGroups.Count > 0) Then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RenderOptionGroupEndTag(writer) 'need to close previous group&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; End If&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RenderOptionGroupBeginTag(currentOptionGroup, writer)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; renderedOptionGroups.Add(currentOptionGroup)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; End If&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RenderListItem(item, writer)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ElseIf item.Text = "--" Then 'simple separator&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RenderOptionGroupBeginTag("--", writer)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RenderOptionGroupEndTag(writer)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'default behavior: render the list item as normal&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RenderListItem(item, writer)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; End If&nbsp; &nbsp; &nbsp; &nbsp; Next item&nbsp; &nbsp; &nbsp; &nbsp; If renderedOptionGroups.Count > 0 Then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RenderOptionGroupEndTag(writer)&nbsp; &nbsp; &nbsp; &nbsp; End If&nbsp; &nbsp; End Sub&nbsp; &nbsp; Private Sub RenderOptionGroupBeginTag(ByVal name As String, ByVal writer As HtmlTextWriter)&nbsp; &nbsp; &nbsp; &nbsp; writer.WriteBeginTag("optgroup")&nbsp; &nbsp; &nbsp; &nbsp; writer.WriteAttribute("label", name)&nbsp; &nbsp; &nbsp; &nbsp; writer.Write(HtmlTextWriter.TagRightChar)&nbsp; &nbsp; &nbsp; &nbsp; writer.WriteLine()&nbsp; &nbsp; End Sub&nbsp; &nbsp; Private Sub RenderOptionGroupEndTag(ByVal writer As HtmlTextWriter)&nbsp; &nbsp; &nbsp; &nbsp; writer.WriteEndTag("optgroup")&nbsp; &nbsp; &nbsp; &nbsp; writer.WriteLine()&nbsp; &nbsp; End Sub&nbsp; &nbsp; Private Sub RenderListItem(ByVal item As ListItem, ByVal writer As HtmlTextWriter)&nbsp; &nbsp; &nbsp; &nbsp; writer.WriteBeginTag("option")&nbsp; &nbsp; &nbsp; &nbsp; writer.WriteAttribute("value", item.Value, True)&nbsp; &nbsp; &nbsp; &nbsp; If item.Selected Then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.WriteAttribute("selected", "selected", False)&nbsp; &nbsp; &nbsp; &nbsp; End If&nbsp; &nbsp; &nbsp; &nbsp; For Each key As String In item.Attributes.Keys&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.WriteAttribute(key, item.Attributes(key))&nbsp; &nbsp; &nbsp; &nbsp; Next key&nbsp; &nbsp; &nbsp; &nbsp; writer.Write(HtmlTextWriter.TagRightChar)&nbsp; &nbsp; &nbsp; &nbsp; HttpUtility.HtmlEncode(item.Text, writer)&nbsp; &nbsp; &nbsp; &nbsp; writer.WriteEndTag("option")&nbsp; &nbsp; &nbsp; &nbsp; writer.WriteLine()&nbsp; &nbsp; End SubEnd Class这是同一类的C#实现:/* This codes makes the dropdownlist control recognize items with "--"&nbsp;* for the label or items with an OptionGroup attribute and render them&nbsp;* as <optgroup> instead of <option>.&nbsp;*/public class DropDownListAdapter : WebControlAdapter{&nbsp; &nbsp; protected override void RenderContents(HtmlTextWriter writer)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; //System.Web.HttpContext.Current.Response.Write("here");&nbsp; &nbsp; &nbsp; &nbsp; var list = (DropDownList)this.Control;&nbsp; &nbsp; &nbsp; &nbsp; string currentOptionGroup;&nbsp; &nbsp; &nbsp; &nbsp; var renderedOptionGroups = new List<string>();&nbsp; &nbsp; &nbsp; &nbsp; foreach (ListItem item in list.Items)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Page.ClientScript.RegisterForEventValidation(list.UniqueID, item.Value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Is the item part of an option group?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (item.Attributes["OptionGroup"] != null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentOptionGroup = item.Attributes["OptionGroup"];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Was the option header already written, then just render the list item&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (renderedOptionGroups.Contains(currentOptionGroup))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RenderListItem(item, writer);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //The header was not written,do that first&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Close previous group&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (renderedOptionGroups.Count > 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RenderOptionGroupEndTag(writer);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RenderOptionGroupBeginTag(currentOptionGroup, writer);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; renderedOptionGroups.Add(currentOptionGroup);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RenderListItem(item, writer);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Simple separator&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (item.Text == "--")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RenderOptionGroupBeginTag("--", writer);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RenderOptionGroupEndTag(writer);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Default behavior, render the list item as normal&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RenderListItem(item, writer);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (renderedOptionGroups.Count > 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RenderOptionGroupEndTag(writer);&nbsp; &nbsp; }&nbsp; &nbsp; private void RenderOptionGroupBeginTag(string name, HtmlTextWriter writer)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; writer.WriteBeginTag("optgroup");&nbsp; &nbsp; &nbsp; &nbsp; writer.WriteAttribute("label", name);&nbsp; &nbsp; &nbsp; &nbsp; writer.Write(HtmlTextWriter.TagRightChar);&nbsp; &nbsp; &nbsp; &nbsp; writer.WriteLine();&nbsp; &nbsp; }&nbsp; &nbsp; private void RenderOptionGroupEndTag(HtmlTextWriter writer)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; writer.WriteEndTag("optgroup");&nbsp; &nbsp; &nbsp; &nbsp; writer.WriteLine();&nbsp; &nbsp; }&nbsp; &nbsp; private void RenderListItem(ListItem item, HtmlTextWriter writer)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; writer.WriteBeginTag("option");&nbsp; &nbsp; &nbsp; &nbsp; writer.WriteAttribute("value", item.Value, true);&nbsp; &nbsp; &nbsp; &nbsp; if (item.Selected)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.WriteAttribute("selected", "selected", false);&nbsp; &nbsp; &nbsp; &nbsp; foreach (string key in item.Attributes.Keys)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.WriteAttribute(key, item.Attributes[key]);&nbsp; &nbsp; &nbsp; &nbsp; writer.Write(HtmlTextWriter.TagRightChar);&nbsp; &nbsp; &nbsp; &nbsp; HttpUtility.HtmlEncode(item.Text, writer);&nbsp; &nbsp; &nbsp; &nbsp; writer.WriteEndTag("option");&nbsp; &nbsp; &nbsp; &nbsp; writer.WriteLine();&nbsp; &nbsp; }}我的浏览器文件名为“ App_Browsers \ BrowserFile.browser”,看起来像这样:<!--&nbsp; &nbsp; You can find existing browser definitions at&nbsp; &nbsp; <windir>\Microsoft.NET\Framework\<ver>\CONFIG\Browsers--><browsers>&nbsp; &nbsp;<browser refID="Default">&nbsp; &nbsp; &nbsp; <controlAdapters>&nbsp; &nbsp; &nbsp; &nbsp; <adapter controlType="System.Web.UI.WebControls.DropDownList"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;adapterType="DropDownListAdapter" />&nbsp; &nbsp; &nbsp; </controlAdapters>&nbsp; &nbsp;</browser></browsers>

慕慕森

我使用JQuery来完成此任务。我首先为ListItem后端的每个对象添加了一个新属性,然后在JQuery wrapAll()方法中使用该属性来创建组...C#:foreach (ListItem item in ((DropDownList)sender).Items){&nbsp; &nbsp; if (System.Int32.Parse(item.Value) < 5)&nbsp; &nbsp; &nbsp; &nbsp; item.Attributes.Add("classification", "LessThanFive");&nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; item.Attributes.Add("classification", "GreaterThanFive");}&nbsp;jQuery的:$(document).ready(function() {&nbsp; &nbsp; //Create groups for dropdown list&nbsp; &nbsp; $("select.listsmall option[@classification='LessThanFive']")&nbsp; &nbsp; &nbsp; &nbsp; .wrapAll("&lt;optgroup label='Less than five'&gt;");&nbsp; &nbsp; $("select.listsmall option[@classification='GreaterThanFive']")&nbsp; &nbsp; &nbsp; &nbsp; .wrapAll("&lt;optgroup label='Greater than five'&gt;");&nbsp;});

扬帆大鱼

这里是C#版本:using System;using System.Web.UI.WebControls.Adapters;using System.Web.UI;using System.Web.UI.WebControls;using System.Collections.Generic;using System.Web;//This codes makes the dropdownlist control recognize items with "--"'//for the label or items with an OptionGroup attribute and render them'//as&nbsp; instead of .'public class DropDownListAdapter : WebControlAdapter{&nbsp; &nbsp; protected override void RenderContents(HtmlTextWriter writer)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; DropDownList list = Control as DropDownList;&nbsp; &nbsp; &nbsp; &nbsp; string currentOptionGroup;&nbsp; &nbsp; &nbsp; &nbsp; List renderedOptionGroups = new List();&nbsp; &nbsp; &nbsp; &nbsp; foreach(ListItem item in list.Items)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (item.Attributes["OptionGroup"] != null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //'The item is part of an option group'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentOptionGroup = item.Attributes["OptionGroup"];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //'the option header was already written, just render the list item'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(renderedOptionGroups.Contains(currentOptionGroup))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RenderListItem(item, writer);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //the header was not written- do that first'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (renderedOptionGroups.Count > 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RenderOptionGroupEndTag(writer); //'need to close previous group'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RenderOptionGroupBeginTag(currentOptionGroup, writer);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; renderedOptionGroups.Add(currentOptionGroup);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RenderListItem(item, writer);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (item.Text == "--") //simple separator&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RenderOptionGroupBeginTag("--", writer);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RenderOptionGroupEndTag(writer);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //default behavior: render the list item as normal'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RenderListItem(item, writer);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if(renderedOptionGroups.Count > 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RenderOptionGroupEndTag(writer);&nbsp; &nbsp; }&nbsp; &nbsp; private void RenderOptionGroupBeginTag(string name, HtmlTextWriter writer)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; writer.WriteBeginTag("optgroup");&nbsp; &nbsp; &nbsp; &nbsp; writer.WriteAttribute("label", name);&nbsp; &nbsp; &nbsp; &nbsp; writer.Write(HtmlTextWriter.TagRightChar);&nbsp; &nbsp; &nbsp; &nbsp; writer.WriteLine();&nbsp; &nbsp; }&nbsp; &nbsp; private void RenderOptionGroupEndTag(HtmlTextWriter writer)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; writer.WriteEndTag("optgroup");&nbsp; &nbsp; &nbsp; &nbsp; writer.WriteLine();&nbsp; &nbsp; }&nbsp; &nbsp; private void RenderListItem(ListItem item, HtmlTextWriter writer)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; writer.WriteBeginTag("option");&nbsp; &nbsp; &nbsp; &nbsp; writer.WriteAttribute("value", item.Value, true);&nbsp; &nbsp; &nbsp; &nbsp; if (item.Selected)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.WriteAttribute("selected", "selected", false);&nbsp; &nbsp; &nbsp; &nbsp; foreach (string key in item.Attributes.Keys)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.WriteAttribute(key, item.Attributes[key]);&nbsp; &nbsp; &nbsp; &nbsp; writer.Write(HtmlTextWriter.TagRightChar);&nbsp; &nbsp; &nbsp; &nbsp; HttpUtility.HtmlEncode(item.Text, writer);&nbsp; &nbsp; &nbsp; &nbsp; writer.WriteEndTag("option");&nbsp; &nbsp; &nbsp; &nbsp; writer.WriteLine();&nbsp; &nbsp; }}
随时随地看视频慕课网APP
我要回答