ASP.NET 2.0, for all its bells and whistles, lacks the odd bit of functionality for reasons completely unknown. One such notable omission is that of OptionGroup
(<optgroup>
) support in the DropDownList
control. For those unfamiliar with the <optgroup>
element, it is part of the XHTML standard, and has the effect of categorising items in a <select>
, as the following image shows.Control Adapters are new in ASP.NET 2.0, and allow the developer to override the rendering behavior of any control, very powerful stuff! Furthermore, Control Adapters are used in conjunction with a browser file, so specific browsers may be targeted, if required. Armed with that knowledge, the solution became simple. The attached download contains the requisite files to implement this solution in your own projects, but for posterity, I paste it here also.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | public class DropDownListAdapter : System.Web.UI.WebControls.Adapters.WebControlAdapter { protected override void RenderContents(HtmlTextWriter writer) { DropDownList list = this .Control as DropDownList; string currentOptionGroup; List< string > 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); } } 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(); } } </ string ></ string > |
1 |
Also, you have to hook it up using a .browserfile in your project. My browser file was named "App_Browsers\BrowserFile.browser" and looked like this:
You can find existing browser definitions at
[windir]\Microsoft.NET\Framework\[ver]\CONFIG\Browsers
1 2 3 4 5 6 7 | <browsers> <browser refid= "Default" > <controladapters> <adapter adaptertype= "DropDownListAdapter" controltype= "System.Web.UI.WebControls.DropDownList" > </adapter></controladapters> </browser> </browsers> |
Binding DropDown List from DataTable
1 2 3 | <asp:dropdownlist id= "Select_DDL" runat= "server" width= "100%" > <asp:listitem>Select</asp:listitem> </asp:dropdownlist> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | void BindDropDownList() { Select_DDL.DataTextField = "DDL_Name" ; Select_DDL.DataValueField = " DDL _Code" ; Select_DDL.DataSource = dsAllData.Tables[1]; Select_DDL.DataBind(); string optionGroup = string .Empty; for ( int i = 0; i < dsAllData.Tables[1].Rows.Count; i++) { if (dsAllData.Tables[1].Rows[i][ "parent" ].ToString() == "0" ) optionGroup = dsAllData.Tables[1].Rows[i][ "Product_Name" ].ToString(); Select_DDL.Items[i].Attributes[ "OptionGroup" ] = optionGroup; } int count = Select_DDL.Items.Count; for ( int i = count - 1; i >= 0; i--) { if (dsAllData.Tables[1].Rows[i][ "parent" ].ToString() == "0" ) { Select_DDL.Items.RemoveAt(i); } } } |
Comments
Post a Comment