ObjectDataSourceとDropDownListの組み合わせで先頭に空項目を追加する時

聞かれたんで小ネタ。
ObjectDataSourceとDropDownListの組み合わせで、DropDownListの先頭に空の項目を追加したい場合、下記の様なDropDownList拡張を用意して標準のDropDownListを置き換えるのが楽かな(・∀・)

public class DropDownList : System.Web.UI.WebControls.DropDownList
{
    public bool InsertBlank { get; set; }
    public string BlankText { get; set; }
    public string BlankValue { get; set; }

    public DropDownList()
    {
        DataBound += OnDataBound;
    }

    void OnDataBound(object sender, EventArgs e)
    {
        if( InsertBlank )
        {
            Items.Insert( 0, new ListItem( BlankText, BlankValue ) );
        }
    }

    protected override void PerformDataBinding(System.Collections.IEnumerable dataSource)
    {
        try
        {
            base.PerformDataBinding( dataSource );
        }
        catch
        {
            // 対象が無い場合は先頭を表示
        }
    }
}

データソースを提供する処理を弄るのも変だし、個々にDataBound書くのも面倒なので、拡張クラスで対応する方向で。


てっきりなんかプロパティがあったと思いこんでいたんだけど、なにと勘違いしていたんだろう(・ω・)?