Adding a DataDefaultField to a DropDownList

At times you want to return the default to show in an asp:DropDownList from the DataSource. Normally you have only two items:

  • DataTextField
  • DataValueField.

I will illustrate how you can add and use a DataDefaultField (boolean) to select a specific item without needing to do any C# in the page.

 

The steps are:

  1. Create a class that inherits from DropDownList
  2. Add in two private variable, defaultValue, isPostBack
  3. Code Snippet
    1. public class DropDownList : System.Web.UI.WebControls.DropDownList
    2. {
    3.   private string defaultValue;
    4.   private bool isPostback ;
  4. Do an override as shown below to detect PostBacks
    Code Snippet
    1. protected override void RaisePostDataChangedEvent()
    2.  {
    3.    isPostback = true;
    4.    base.RaisePostDataChangedEvent();
    5.  }
  5. Add a new Property
    Code Snippet
    1. public virtual string DataDefaultField { get; set; }
  6. Add another override (this picks up the (last) default value from the data.
    Code Snippet
    1. protected override void PerformDataBinding(IEnumerable data)
    2. {           
    3.   if (string.IsNullOrWhiteSpace(DataDefaultField))
    4.   {
    5.     DataDefaultField = "default";
    6.   }
    7.  
    8.   foreach (var dataItem in data)
    9.   {
    10.     bool isDefault;
    11.     if (bool.TryParse(dataItem.GetProperty(DataDefaultField), out isDefault) && isDefault)
    12.     {
    13.       defaultValue = dataItem.GetProperty(DataValueField);
    14.     }
    15.   }
    16.   base.PerformDataBinding(data);
    17. }
  7. Add the last override which sets the default value IF it is not a postback
  8. Code Snippet
    1. public override void RenderControl(HtmlTextWriter writer)
    2. {
    3.   if (! isPostback && !string.IsNullOrWhiteSpace(defaultValue))
    4.   {
    5.     var defaultItem = Items.FindByValue(defaultValue);
    6.     if (defaultItem != null)
    7.     {
    8.       SelectedIndex = -1;
    9.       defaultItem.Selected = true;
    10.     }
    11.   }
    12.   base.RenderControl(writer);
    13. }

That’s it. You can now set the default value in the datasource.

 

Oops.. Bill just pointed out that I used extensions that I did not include.. Here there are:

 

Code Snippet
  1. public static string GetProperty(this object dataItem, string field, string defaultValue)
  2.   {
  3.   if (string.IsNullOrWhiteSpace(field))
  4.     {
  5.     return defaultValue;
  6.     }
  7.  
  8.   var props = TypeDescriptor.GetProperties(dataItem);
  9.   if (props.Count > 0)
  10.     {
  11.     if (props[field] != null && null != props[field].GetValue(dataItem))
  12.       {
  13.       return props[field].GetValue(dataItem).ToString();
  14.       }
  15.  
  16.     for (var i = 0; i < props.Count; i++)
  17.       {
  18.       if (string.Compare(props[i].Name, field, StringComparison.OrdinalIgnoreCase) == 0)
  19.         {
  20.         return props[i].GetValue(dataItem).ToString();
  21.         }
  22.       }
  23.     }
  24.  
  25.   return null;
  26.   }

and

Code Snippet
  1. public static string GetProperty(this object dataItem, string field)
  2.   {
  3.   return GetProperty(dataItem, field, null);
  4.   }

Comments

Popular posts from this blog

Yet once more into the breech (of altered programming logic)

Simple WP7 Mango App for Background Tasks, Toast, and Tiles: Code Explanation

How to convert SVG data to a Png Image file Using InkScape