重写 DropDownList SelectedValue 解决 有一个无效 SelectedValue,因为它不在项目列表中...

-

  使用DropDownList绑定数据时,有时候会出现问题:有一个无效 SelectedValue,因为它不在项目列表中

  产生这个问题的原因是,需绑定的SelectedValue值,在DropDownList中不存在。所以系统抛出异常:

  异常详细信息: System.ArgumentOutOfRangeException: “SupplierDrpDwnLst”有一个无效 SelectedValue,因为它不在项目列表中。
  参数名: value

  本人搜索了好多文章,解决方法都是用

  DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue("XXX"));

   这种方法不错,但是,每次都要在cs文件中写代码,极不方便。有没有办法,不用在后台写代码呢。也就是,我们重写一个DropDownList控件,当发现错误时,处理错误。

   经过本人不懈的搜索,终于找到了重写的方法

 

ContractedBlock.gif ExpandedBlockStart.gif Code
  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
 15ExpandedBlockStart.gifContractedBlock.gif{
 16ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <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
 30ExpandedSubBlockStart.gifContractedSubBlock.gif{
 31ContractedSubBlock.gifExpandedSubBlockStart.gifprivate fields#region private fields
 32
 33private string cachedSelectedValue;
 34
 35#endregion
 private fields
 36
 37ContractedSubBlock.gifExpandedSubBlockStart.gifConstructor#region Constructor
 38
 39public ExtendedDropDownList()
 40ExpandedSubBlockStart.gifContractedSubBlock.gif{
 41}

 42
 43#endregion
 Constructor
 44
 45ContractedSubBlock.gifExpandedSubBlockStart.gifProperties#region Properties
 46[Themeable(false), DefaultValue("Old Value")]
 47public virtual string ObsoleteValueText
 48ExpandedSubBlockStart.gifContractedSubBlock.gif{
 49get
 50ExpandedSubBlockStart.gifContractedSubBlock.gif{
 51object obj2 = this.ViewState["ObsoleteValueText"];
 52if (obj2 != null && !string.IsNullOrEmpty(obj2.ToString()))
 53ExpandedSubBlockStart.gifContractedSubBlock.gif{
 54return (string)obj2;
 55}

 56return "Item archived";
 57}

 58set
 59ExpandedSubBlockStart.gifContractedSubBlock.gif{
 60this.ViewState["ObsoleteValueText"= value;
 61//if (base.Initialized)
 62//{
 63//    base.RequiresDataBinding = true;
 64//}
 65}

 66}

 67
 68public override string SelectedValue
 69ExpandedSubBlockStart.gifContractedSubBlock.gif{
 70get
 71ExpandedSubBlockStart.gifContractedSubBlock.gif{
 72return base.SelectedValue;
 73}

 74set
 75ExpandedSubBlockStart.gifContractedSubBlock.gif{
 76try
 77ExpandedSubBlockStart.gifContractedSubBlock.gif{
 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}

 88ExpandedSubBlockStart.gifContractedSubBlock.gifcatch { }
 89}

 90}

 91
 92#endregion
 Properties
 93
 94ContractedSubBlock.gifExpandedSubBlockStart.gifHandle ArgumentOutOfRangeException#region Handle ArgumentOutOfRangeException
 95
 96protected override void PerformDataBinding(System.Collections.IEnumerable dataSource)
 97ExpandedSubBlockStart.gifContractedSubBlock.gif{
 98string selectedValue = this.SelectedValue;
 99try
100ExpandedSubBlockStart.gifContractedSubBlock.gif{
101base.PerformDataBinding(dataSource);
102}

103catch (ArgumentOutOfRangeException)
104ExpandedSubBlockStart.gifContractedSubBlock.gif{
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。跟我想的方法一样,不过本人水平有限,自己写不出来。

本人改造效果图

 

DropDownList-override-string-SelectedValue-1
DropDownList-override-string-SelectedValue-2

转载于:https://www.cnblogs.com/howgoo/archive/2008/12/09/DropDownList-SelectedValue-ArgumentOutOfRangeException-override-string-SelectedValue.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值