使用DropDownList绑定数据时,有时候会出现问题:有一个无效 SelectedValue,因为它不在项目列表中
产生这个问题的原因是,需绑定的SelectedValue值,在DropDownList中不存在。所以系统抛出异常:
异常详细信息: System.ArgumentOutOfRangeException: “SupplierDrpDwnLst”有一个无效 SelectedValue,因为它不在项目列表中。
参数名: value
本人搜索了好多文章,解决方法都是用
DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue("XXX"));
这种方法不错,但是,每次都要在cs文件中写代码,极不方便。有没有办法,不用在后台写代码呢。也就是,我们重写一个DropDownList控件,当发现错误时,处理错误。
经过本人不懈的搜索,终于找到了重写的方法
1using System;
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10
11using System.ComponentModel;
12using System.Collections;
13
14namespace Safi.UI
15{
16/**//// <summary>
17/// An enhanced DropDownList which traps the SelectedValueNotInList exception.
18/// </summary>
19/// <remarks>
20/// When a nested DropDownList is bound to a datasource and to a SelectedValue, and
21/// the SelectedValue does not appear in the list returned by the datasource, the
22/// ASP.NET throws an ArgumentOutOfRangeException that the consuming code cannot trap
23/// and deal with. None of the available event handlers (apparently) enable this situation
24/// either to be avoided or caught.
25/// This Implementation inherits the DropDownList and overrides the offending method, wrapping
26/// a call to the base method in a try catch block, and handling the error.
27/// </remarks>
28/// <seealso cref="http://weblogs.asp.net/hpreishuber/archive/2005/10/11/427266.aspx"/>
29public class ExtendedDropDownList : System.Web.UI.WebControls.DropDownList
30{
31private fields#region private fields
32
33private string cachedSelectedValue;
34
35#endregion private fields
36
37Constructor#region Constructor
38
39public ExtendedDropDownList()
40{
41}
42
43#endregion Constructor
44
45Properties#region Properties
46[Themeable(false), DefaultValue("Old Value")]
47public virtual string ObsoleteValueText
48{
49get
50{
51object obj2 = this.ViewState["ObsoleteValueText"];
52if (obj2 != null && !string.IsNullOrEmpty(obj2.ToString()))
53{
54return (string)obj2;
55}
56return "Item archived";
57}
58set
59{
60this.ViewState["ObsoleteValueText"] = value;
61//if (base.Initialized)
62//{
63// base.RequiresDataBinding = true;
64//}
65}
66}
67
68public override string SelectedValue
69{
70get
71{
72return base.SelectedValue;
73}
74set
75{
76try
77{
78base.SelectedValue = value;
79// This is the important bit. The base DropDownList overwrites the SelectedValue property during binding.
80// It uses a private cachedSelectedValue variable to track the original value.
81// That variable is written to in the SelectedValue setter.
82// However the code (in base.PerformDataBinding) that throws the ArgumentOutOfRangeException
83// will also write SelectedValue to "0". We only want to get a copy of the "real" SelectedValue.
84// This solution is a HACK, but the best I can come up with just now.
85if (value != "0")
86cachedSelectedValue = value;
87}
88catch { }
89}
90}
91
92#endregion Properties
93
94Handle ArgumentOutOfRangeException#region Handle ArgumentOutOfRangeException
95
96protected override void PerformDataBinding(System.Collections.IEnumerable dataSource)
97{
98string selectedValue = this.SelectedValue;
99try
100{
101base.PerformDataBinding(dataSource);
102}
103catch (ArgumentOutOfRangeException)
104{
105ListItem item = new ListItem();
106item.Value = cachedSelectedValue;
107item.Text = this.ObsoleteValueText;
108this.Items.Add(item);
109this.SelectedIndex = this.Items.IndexOf(this.Items.FindByValue(cachedSelectedValue));
110}
111}
112
113#endregion Handle ArgumentOutOfRangeException
114
115}
116}
原文地址:http://david.safitech.com/?p=53。跟我想的方法一样,不过本人水平有限,自己写不出来。
本人改造效果图