ASP.NET中DropDownList设置SelectedValue触发ArgumentOutOfRangeException
ASP.NET DropDownList SelectedValue 触发 ArgumentOutOfRangeException 问题排查与解决
问题现象
开发ASP.NET Web应用时,GridView进入编辑模式后,DropDownList控件设置SelectedValue时触发System.ArgumentOutOfRangeException,错误提示:
'DropDownListRab' 的SelectedValue无效,因为它不在项列表中
数据库中[Rab, %]列的值为NULL、10.00、20.00等,GridView中该列显示为30,00%、20,00%格式,且已确认DropDownList的列表项包含对应值,但仍触发异常。
相关代码
GridView RowEditing 事件
Protected Sub GridViewItems_RowEditing(sender As Object, e As GridViewEditEventArgs) ' 将行置于编辑模式 GridViewItems.EditIndex = e.NewEditIndex ' 重新绑定GridView以显示编辑模式的行 Dim orderID As Integer = Integer.Parse(Request.QueryString("orderID")) GridViewItems.DataSource = GetOrderItems(orderID) GridViewItems.DataBind() ' 异常发生在上述DataBind()行 End Sub
折扣Label绑定代码
... Dim discount As Decimal? = If(row("Rab, %") Is DBNull.Value, Nothing, CType(row("Rab, %"), Decimal?)) ' 设置折扣Label控件的文本 Dim labelDiscount As Label = CType(e.Row.FindControl("LabelDiscount"), Label) If discount.HasValue Then labelDiscount.Text = discount.Value.ToString() & "%" Else labelDiscount.Text = "0%" End If ...
DropDownList 标记
<asp:DropDownList ID="DropDownListRab" runat="server" SelectedValue='<%# Eval("[Rab, %]") %>'> <asp:ListItem Text="0%" Value=""></asp:ListItem> <asp:ListItem Text="10%" Value="10.00"></asp:ListItem> <asp:ListItem Text="20%" Value="20.00"></asp:ListItem> <asp:ListItem Text="30%" Value="30.00"></asp:ListItem> <asp:ListItem Text="40%" Value="40.00"></asp:ListItem> <asp:ListItem Text="50%" Value="50.00"></asp:ListItem> <asp:ListItem Text="60%" Value="60.00"></asp:ListItem> <asp:ListItem Text="70%" Value="70.00"></asp:ListItem> <asp:ListItem Text="80%" Value="80.00"></asp:ListItem> <asp:ListItem Text="90%" Value="90.00"></asp:ListItem> <asp:ListItem Text="100%" Value="100.00"></asp:ListItem> </asp:DropDownList>
原因分析
- 空值类型不匹配:数据库中的NULL值通过
Eval返回DBNull.Value,而DropDownList对应空值的项Value为字符串"",两者类型不同,导致ASP.NET无法匹配到对应项。 - 数值与字符串类型差异:数据库中
[Rab, %]是Decimal类型,Eval后返回Decimal值,而DropDownList的Value是字符串格式(如"30.00"),直接绑定会因类型不匹配导致比较失败,无法找到对应项。 - 绑定逻辑未处理格式转换:直接使用
Eval("[Rab, %]")绑定SelectedValue时,Decimal值的字符串表示可能与DropDownList的Value格式不完全一致(如Decimal的30.00可能被转为"30"而非"30.00"),导致匹配失败。
解决方法
方法一:修改Eval绑定,统一值格式
修改DropDownList的SelectedValue绑定表达式,手动处理空值并将Decimal值转换为与DropDownList Value一致的字符串格式:
<asp:DropDownList ID="DropDownListRab" runat="server" SelectedValue='<%# If(Eval("[Rab, %]") Is DBNull.Value, "", CDec(Eval("[Rab, %]")).ToString("0.00")) %>'> <!-- 列表项保持不变 --> </asp:DropDownList>
这样空值对应字符串"",Decimal值会被格式化为"X.XX"的字符串,与DropDownList的Value完全匹配。
方法二:在RowDataBound事件中手动绑定
移除DropDownList标记中的SelectedValue绑定,转而在GridView的RowDataBound事件中手动处理编辑行的DropDownList值绑定:
Protected Sub GridViewItems_RowDataBound(sender As Object, e As GridViewRowEventArgs) ' 仅处理编辑模式的行 If (e.Row.RowState And DataControlRowState.Edit) = DataControlRowState.Edit Then Dim dataItem As Object = e.Row.DataItem Dim discount As Decimal? = If(DataBinder.Eval(dataItem, "[Rab, %]") Is DBNull.Value, Nothing, CDec(DataBinder.Eval(dataItem, "[Rab, %]"))) Dim ddlRab As DropDownList = CType(e.Row.FindControl("DropDownListRab"), DropDownList) If discount.HasValue Then ddlRab.SelectedValue = discount.Value.ToString("0.00") Else ddlRab.SelectedValue = "" End If End If End Sub
这种方式更灵活,能精准控制值的转换和匹配逻辑。
方法三:验证数值格式一致性
检查数据库Decimal值转换为字符串后的格式,确保与DropDownList的Value完全一致。例如,确认Decimal值30.00转换为字符串后是"30.00"而非"30"或"30,00"(注意小数点和逗号的区别),避免因格式差异导致匹配失败。
内容的提问来源于stack exchange,提问作者meraki_1
相关产品推荐
相关产品推荐

