This post is ment to provide a solution to people who want to use a DropDownList control inside a FormView or a GridView control that is bound data sources that may produce unpredictable values. By unpredicatable I mean values that the programmer has not
anticipated to provide in the items that populate the DropDownList.
Failure to provide the bound value in the list of the items in the DropDownList will result in a
ArgumentOutOfRangeException
inside the void OnDataBinding(EventArgs e) function. This problem has been bothering me for a long time but now I think that I have the best solution
that I would like to share with you. I have seen many posts inquiring for a solution to this problem so I think this post will be benefitial.
The best solution to the problem as I have form a few other posts is to create a custom server conrol. Most people the put it in a Control Library. For the time being I have it in my ~/App_Code
folder. Here is the file
The DataDropDownList is defined in
namespace Custom.Controls but you can put it in any namespace you want. It extends the original asp
DropDownList and adds two properties: The DataBoundField is the name of the field that is bound to by the
SelectedValue='<%# Bind("field") %>'
original. There is a bit of repetition here since the name of the field is used twice but it works well. I am using the DataBoundField to read
the value that caused the ArgumentOutOfRangeException
inside data binding. The second property NotInListPrompt is a string that will be inserted in the list in place of the unexpected value. By default it is "{0}" which will insert the unexpected value to be inserted in the list. The control
is overriding the protected
void OnDataBinding(EventArgs e) method which will call the base method and will catch the
ArgumentOutOfRangeException.
None
0 Points
14 Posts
How to handle databound DropDownList controls whose bound value is not in the items' list
Sep 29, 2007 07:41 PM|attractor|LINK
This post is ment to provide a solution to people who want to use a DropDownList control inside a FormView or a GridView control that is bound data sources that may produce unpredictable values. By unpredicatable I mean values that the programmer has not anticipated to provide in the items that populate the DropDownList.
Failure to provide the bound value in the list of the items in the DropDownList will result in a ArgumentOutOfRangeException inside the void OnDataBinding(EventArgs e) function. This problem has been bothering me for a long time but now I think that I have the best solution that I would like to share with you. I have seen many posts inquiring for a solution to this problem so I think this post will be benefitial.
The best solution to the problem as I have form a few other posts is to create a custom server conrol. Most people the put it in a Control Library. For the time being I have it in my ~/App_Code folder. Here is the file
// DataDropDownList.csusing System;using System.Data;using System.ComponentModel;using System.Security.Permissions;using System.Web;using System.Web.UI;using System.Web.UI.WebControls; namespace Custom.Controls { [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal), AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal), ToolboxData("<{0}:DataDropDownList runat=\"server\"> </{0}:DataDropDownList>")] public class DataDropDownList : DropDownList { private string boundFieldName, notInListPrompt; public DataDropDownList() { boundFieldName = ""; notInListPrompt = "{0}"; } [Bindable(false), Category("Data"), DefaultValue("")] public string DataBoundField { get { return boundFieldName; } set { boundFieldName = value; } } [Bindable(false), Category("Appearance"), DefaultValue("{0}")] public string NotInListPrompt { get { return notInListPrompt; } set { notInListPrompt = value; } } protected override void OnDataBinding(EventArgs e) { try { base.OnDataBinding(e); } catch (ArgumentOutOfRangeException ex) { IDataItemContainer dr = (IDataItemContainer)this.NamingContainer; DataRowView drv = (DataRowView)dr.DataItem; string v = drv[this.DataBoundField].ToString(); ListItem li = new ListItem(String.Format(notInListPrompt, v), v); li.Selected = true; this.Items.Add(li); } } }}
The DataDropDownList is defined in namespace Custom.Controls but you can put it in any namespace you want. It extends the original asp DropDownList and adds two properties: The DataBoundField is the name of the field that is bound to by the SelectedValue='<%# Bind("field") %>' original. There is a bit of repetition here since the name of the field is used twice but it works well. I am using the DataBoundField to read the value that caused the ArgumentOutOfRangeException inside data binding. The second property NotInListPrompt is a string that will be inserted in the list in place of the unexpected value. By default it is "{0}" which will insert the unexpected value to be inserted in the list. The control is overriding the protected void OnDataBinding(EventArgs e) method which will call the base method and will catch the ArgumentOutOfRangeException.