Click here to Skip to main content
15,886,519 members
Articles / Web Development / ASP.NET

DropDownList with OptionGroup

Rate me:
Please Sign up or sign in to vote.
4.79/5 (24 votes)
19 Jun 2008CPOL3 min read 97.7K   1.6K   40  
An ASP.NET DropDownList custom control with the HTML OptionGroup feature.
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.Design;

namespace OptionDropDownList
{
  /// <summary>
  /// The ControlDesigner class provides a base control designer class that can be inherited 
  /// from and extended to provide design-time support for a Web server control in a design host, 
  /// such as Visual Studio 2005
  /// 
  /// IDataSourceProvider: This interface specifies the behavior required for designers 
  /// that interact with a data source. The interface provides two methods designed to convert a 
  /// data source to a more useful object. 
  /// 
  /// GetSelectedDataSource(): 
  /// retrieves the selected data source object as a loosely typed System.Object. 
  /// 
  /// GetResolvedSelectedDataSource():
  /// retrieves the resolved data source as a System.Collections.IEnumerable object, 
  /// like a System.Array or a System.Data.DataView instance
  /// </summary>
  public class OptionGroupSelectControlDesigner : ControlDesigner
  {
    OptionGroupSelect relatedControl = null;

    /// <summary>
    /// Initialize
    /// </summary>
    /// <param name="component"></param>
    public override void Initialize(System.ComponentModel.IComponent component)
    {
      this.relatedControl = (OptionGroupSelect)component;
      base.Initialize(component);
    }

    /// <summary>
    /// GetDesignTimeHtml
    /// </summary>
    /// <returns></returns>
    public override string GetDesignTimeHtml()
    {
      try
      {
        if (this.relatedControl.Items == null || this.relatedControl.Items.Count == 0)
        {
          return this.GetEmptyDesignTimeHtml();
        }
        else
        {
          foreach (OptionGroupItem item in this.relatedControl.Items)
            if (item.Selected) return this.DrawControl((item.Text == null ? item.Value : item.Text));

          OptionGroupItem firstitem = this.relatedControl.Items[0] as OptionGroupItem;
          return this.DrawControl(firstitem.Text);
        }
      }
      catch(Exception exception)
      {
        return this.GetErrorDesignTimeHtml(exception);
      }
    }

    /// <summary>
    /// GetEmptyDesignTimeHtml
    /// </summary>
    /// <returns></returns>
    protected override string GetEmptyDesignTimeHtml()
    {
      return this.DrawControl("Empty");
    }

    /// <summary>
    /// GetErrorDesignTimeHtml
    /// </summary>
    /// <param name="e"></param>
    /// <returns></returns>
    protected override string GetErrorDesignTimeHtml(Exception e)
    {
      return "<table border='1' bgcolor='gray'><tr><td><font color='red'>" + e.Message + "</font></td></tr></table>";
    }

    /// <summary>
    /// Set to True so that the control can be resized on the form.
    /// </summary>
    public override bool AllowResize
    {
      get
      {
        return true;
      }
    }

    /// <summary>
    /// DrawControl
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    private String DrawControl(String value)
    {
      StringBuilder renderHtml = new StringBuilder("<select>");
      if(!String.IsNullOrEmpty(value)) renderHtml.Append(String.Format("<option value='{0}'>{0}</option>", value));
      renderHtml.Append("</select>");
      return renderHtml.ToString();
    }
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions