Created
May 27, 2014 12:41
-
-
Save jbubriski/5648fb15fe3a475754cf to your computer and use it in GitHub Desktop.
DropDownList Adapter for ASP.NET to add support for Option Groups. Created from: http://www.codeproject.com/Articles/15505/ASP-NET-DropDownList-with-OptionGroup-support and http://forums.asp.net/t/1172039.aspx?Building+a+Control+Adapter+for+the+CheckBox+control+
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
You can find existing browser definitions at | |
<windir>\Microsoft.NET\Framework\<ver>\CONFIG\Browsers | |
--> | |
<!-- File: MyAdapters.browser --> | |
<browsers> | |
<browser refID="Default"> | |
<controlAdapters> | |
<adapter controlType="System.Web.UI.WebControls.DropDownList" adapterType="DropDownListAdapter" /> | |
</controlAdapters> | |
</browser> | |
</browsers> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Web.UI; | |
using System.Web.UI.WebControls; | |
public class DropDownListAdapter : System.Web.UI.WebControls.Adapters.WebControlAdapter | |
{ | |
protected override void RenderContents(HtmlTextWriter writer) | |
{ | |
DropDownList list = this.Control as DropDownList; | |
if (list != null) | |
{ | |
string currentOptionGroup; | |
var renderedOptionGroups = new List<string>(); | |
foreach (ListItem item in list.Items) | |
{ | |
if (item.Attributes["OptionGroup"] == null) | |
{ | |
RenderListItem(item, writer); | |
} | |
else | |
{ | |
currentOptionGroup = item.Attributes["OptionGroup"]; | |
if (renderedOptionGroups.Contains(currentOptionGroup)) | |
{ | |
RenderListItem(item, writer); | |
} | |
else | |
{ | |
if (renderedOptionGroups.Count > 0) | |
{ | |
RenderOptionGroupEndTag(writer); | |
} | |
RenderOptionGroupBeginTag(currentOptionGroup, writer); | |
renderedOptionGroups.Add(currentOptionGroup); | |
RenderListItem(item, writer); | |
} | |
} | |
} | |
if (renderedOptionGroups.Count > 0) | |
{ | |
RenderOptionGroupEndTag(writer); | |
} | |
} | |
if (Page != null) | |
{ | |
Page.ClientScript.RegisterForEventValidation(Control.UniqueID); | |
} | |
} | |
private void RenderOptionGroupBeginTag(string name, HtmlTextWriter writer) | |
{ | |
writer.WriteBeginTag("optgroup"); | |
writer.WriteAttribute("label", name); | |
writer.Write(HtmlTextWriter.TagRightChar); | |
writer.WriteLine(); | |
} | |
private void RenderOptionGroupEndTag(HtmlTextWriter writer) | |
{ | |
writer.WriteEndTag("optgroup"); | |
writer.WriteLine(); | |
} | |
private void RenderListItem(ListItem item, HtmlTextWriter writer) | |
{ | |
writer.WriteBeginTag("option"); | |
writer.WriteAttribute("value", item.Value, true); | |
if (item.Selected) | |
{ | |
writer.WriteAttribute("selected", "selected", false); | |
} | |
foreach (string key in item.Attributes.Keys) | |
{ | |
writer.WriteAttribute(key, item.Attributes[key]); | |
} | |
writer.Write(HtmlTextWriter.TagRightChar); | |
HttpUtility.HtmlEncode(item.Text, writer); | |
writer.WriteEndTag("option"); | |
writer.WriteLine(); | |
if (Page != null) | |
{ | |
Page.ClientScript.RegisterForEventValidation(this.Control.UniqueID, item.Value); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void BindDropDowns() | |
{ | |
var usa = CountryInfoProvider.GetCountryInfo("USA"); | |
var canada = CountryInfoProvider.GetCountryInfo("Canada"); | |
var usStates = StateInfoProvider.GetStates("CountryID = " + usa.CountryID, "", -1, ""); | |
var canadianStates = StateInfoProvider.GetStates("CountryID = " + canada.CountryID, "", -1, ""); | |
foreach (var state in canadianStates) | |
{ | |
var listItem = new ListItem(state.StateDisplayName, state.StateCode); | |
listItem.Attributes["OptionGroup"] = "Canada"; | |
uxStateSelector.Items.Add(listItem); | |
} | |
foreach (var state in usStates) | |
{ | |
var listItem = new ListItem(state.StateDisplayName, state.StateCode); | |
listItem.Attributes["OptionGroup"] = "USA"; | |
uxStateSelector.Items.Add(listItem); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment