【问题标题】:'ddCountries' has a SelectedValue which is invalid because it does not exist in the list of items'ddCountries' 有一个无效的 SelectedValue,因为它不存在于项目列表中
【发布时间】:2015-01-26 13:02:21
【问题描述】:
<asp:DropDownList runat="server" ValidationGroup="SaveInformation" ID="ddCountries">                                            
</asp:DropDownList>

代码隐藏:带有 ID 和名称的国家/地区将出现在 DataTable 中

ddCountries.DataSource = new  GeneralStatic().GetAllCountries();
ddCountries.DataTextField = "Name";
ddCountries.DataValueField = "ID";
ddCountries.DataBind();
ddCountries.Items.Insert(0, "-- Select Country --");

在抛出错误之前它工作正常

'ddCountries' 有一个无效的 SelectedValue,因为它没有 存在于项目列表中。

【问题讨论】:

    标签: c# asp.net webforms


    【解决方案1】:
            ddCountries.Items.Clear();
            ddCountries.SelectedIndex = -1;
            ddCountries.SelectedValue = null;
            ddCountries.ClearSelection();   
    

    这就是你必须在绑定之前清除项目值和选择的解决方案......

    【讨论】:

      猜你喜欢
      • 2014-01-14
      • 2011-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-10
      • 1970-01-01
      相关资源
      最近更新 更多