Custom ASP.NET DropDownList to Allow Multiple Text Fields

Thursday, July 19, 2007 6:51:12 AM

I had a need to create a DataBound DropDownList today with multiple items as the text field. The problem is that a DropDownList only supports one column for the text field.  If you try to add more than that you will see the error: DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Column1, Column2'.

I have seen this problem posted in many forums, and finally I myself got sick of searching for the solution.  I found one, though the source was not free, so I set out to creat my own.  I figured that it could be too hard.  The trick was, I did not want to to do this with SQL, as I wanted it to be usable with my automatically generated stored procedures from my little O/R Mapper application.

Pulling out reflector I started poking around in the System.Web.UI.DropDownList class.  The method that I wanted to override is the PerformDataBinding method.  I created a new class inheriting System.Web.UI.WebControls.DropDownList and created a new method overriding PerformDataBinding.  I then opened that method up in reflector and copied most of the meat into my new method.

I then removed everything from within the "if (flag)" block about halfway down, this is where the DataTextField is resolved, and replaced it with some new code.  Here is what the new block looks like.
if (flag)
{
    if (dataTextField.Length > 0)
    {
        string[] items = dataTextField.Split(',');
        object[] resolvedItems = new object[items.Length];
        for (int i = 0; i < items.Length; i++)
        {
            string s = items[i];
            s = s.TrimStart(' ');
            s = s.TrimEnd(' ');
            resolvedItems[i] = DataBinder.GetPropertyValue(obj2, s);
        }
        if (dataTextFormatString.Length > 0)
        {
            item.Text = String.Format(dataTextFormatString, resolvedItems);
        }
        else
        {
            item.Text = String.Format("{0}", resolvedItems);
        }
        //item.Text = DataBinder.GetPropertyValue(obj2, dataTextField, dataTextFormatString);
    }
    if (dataValueField.Length > 0)
    {
        item.Value = DataBinder.GetPropertyValue(obj2, dataValueField, null);
    }
}
As you can see from the code, I take the DataTextField property and split the contents, trimming any white space.  I then resolve the databinding on them and place the results into an object[] list to use as a parameter for String.Format.  If the format string is blank I default it to "{0}" to grab the first property.

It isn't a glamorous solution, but it was quick, easy, and now the world can have it for free.

Update:
For those of you looking for a more robust control to do this, that is done for you, eXcentrics World has binaries available for free with a $75 charge for the source code.  I downloaded this, though I haven't had a chance to install it and try it out.

Comments


Leave Comment

  

  

  




Are you human? Prove it!