【问题标题】:DropDownList has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: valueDropDownList 有一个无效的 SelectedValue,因为它不存在于项目列表中。参数名称:值
【发布时间】:2013-11-15 05:14:24
【问题描述】:

我在标题行中不断收到上述错误,这是没有意义的,因为我正在使用一个只有 5 条记录的示例表,并且每条记录都有一个下拉菜单中的值。

这是我用来声明下拉列表的代码。我在我的 SQL 数据源中加入了两个表,以反映我希望在网格视图中填充的内容,并根据需要隐藏列。我正在使用相同的数据源来填充下拉列表,任何帮助将不胜感激

<asp:DropDownList ID="DropDownListCurrency" runat="server" 
                  CausesValidation="True" DataSourceID="GridView" 
                  DataTextField="Currency" DataValueField="Currency_ID" 
                  AppendDataBoundItems="True"> 

          <asp:ListItem Value="0" Text="&lt;Select&gt;" Enabled="True"></asp:ListItem>
</asp:DropDownList>

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    try 
    {
        GridViewRow row = GridView1.SelectedRow; 

        AccountNumber.Text = (string)row.Cells[0].Text;
        ....

        DropDownListCurrency.SelectedValue = (string)row.Cells[8].Text;
    }
    catch (Exception ex)
    {
        Console.WriteLine("{0} Exception caught.", ex);
    }        
}

【问题讨论】:

  • 为什么你的 DataSourceGridView ?这是DataTable的名字吗?
  • 请给我们看代码...
  • @Habib - 我刚刚命名了 DataSource GridView,所以我知道它的用途。原谅我。我对此完全陌生。数据表的名称是 GridView1。
  • @deathismyfriend 参见上面的代码。

标签: c# asp.net sql visual-studio-2010


【解决方案1】:

在尝试设置 SelectedValue 之前尝试在下拉列表中查找值,如下所示:

if (DropDownListCurrency.Items.FindByValue(row.Cells[8].Text.ToString().Trim()) != null)
{
    DropDownListCurrency.SelectedValue = row.Cells[8].Text.ToString().Trim();
}

注意:Trim() 调用将删除文本框文本中的任何前导或尾随空格,这可能是找不到匹配项的原因。

所以你的完整代码应该是这样的:

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    try 
    {
        GridViewRow row = GridView1.SelectedRow; 

        AccountNumber.Text = (string)row.Cells[0].Text;
         ....

        if (DropDownListCurrency.Items.FindByValue(row.Cells[8].Text.ToString().Trim()) != null)
        {
            DropDownListCurrency.SelectedValue = row.Cells[8].Text.ToString().Trim();
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine("{0} Exception caught.", ex);
    }
}

【讨论】:

  • 所以它停止抛出错误 - 非常感谢。但是,它没有被分配到我希望它出现的位置,这与我在输入新记录时使用的区域(下拉列表位置)相同。我在 IF 语句中包含了下面的代码行,因为它曾在它变得如此复杂之前是如何工作的,但现在不再如此了。非常感激任何的帮助。 DropDownListCurrency.Text = DropDownListCurrency.SelectedValue;
  • @New2This - 我建议开始一个新问题,因为它不赞成将“另一个问题”类型的问题附加到问题中。它还通过减少新问题的长度来消除混乱,避免原始问题和通常不相关的问题的混乱,并让人们专注于新问题。
  • 所以当我在我的 aspx 中放入默认选择时,它就停止了工作。
  • Console.WriteLine 在 Web 应用程序中是如何作用的?
【解决方案2】:

用这个就这么简单

Dropdown.SelectedValue = null;
Dropdown.DataBind();

【讨论】:

  • 他们把那个放在哪里?为什么这行得通而他们的原始版本却行不通?添加更多做你的答案,使其真正有用。
【解决方案3】:

您好,您的 gridview 中的那个单元格中可能有空格或下拉列表有空格,例如这不一样

多拉__ = 多拉

多拉尔 = 多拉尔__

在后面的代码中使用 Trim 来清除 SQL Server 中的空格 不要使用 Rtrim 这不是好的做法

【讨论】:

    【解决方案4】:

    Dropdownlist值与数据库列中的值不同。

    示例:下拉菜单显示 Emma,但数据库中存在 Emma 和 Emma1。

    下拉列表找不到 Emma1 的值。

    【讨论】:

      【解决方案5】:

      如果下拉列表中的行位于相关表中并且数据库中未使用参照完整性,则可能会发生这种情况。在我的例子中,我使用参照完整性,但我在下拉列表记录中添加了一个布尔值来表示“已禁用”——即我不再希望用户能够在下拉列表中选择这个值。所以我过滤下拉列表以仅显示未“禁用”的值,但随后的问题是现有数据可能已经包含该值,导致在网格上按下编辑时出现上述错误消息。

      我处理这种情况的方式如下:

      1. 使有问题的下拉列表未绑定以避免错误。
      2. 在后面的代码中添加如下代码:

        protected void StaffTypeGridView_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType != DataControlRowType.DataRow ||
                e.Row.RowIndex != StaffTypeGridView.EditIndex) return;
        
            var staffType = (StaffType)e.Row.DataItem;
            var appCode = staffType.AppCode;
        
            var ddl = (DropDownList) e.Row.FindControl(ddlName);
            if (!string.IsNullOrEmpty(value) &&
                ddl.Items.FindByValue(value) == null)
            {
                ddl.Items.Add(new ListItem
                {
                    Value = value,
                    Text = value + " (Deleted)"
                });
            }
            ddl.SelectedValue = value;
        }
        
      3. 不要忘记在 DropDownList_OnSelectedIndexChanged 或 GridViewOnRowUpdating 中编写代码以将值更新回数据源(作为未绑定字段)。

      4. 向 EditItemTemplate 添加自定义验证器,以确保无法输入已删除的数据,即用户必须更改下拉列表中的值才能保存。

      这听起来很复杂,但提供此功能的方法很简单,除非有人有更好的想法...

      【讨论】:

        【解决方案6】:

        将 ASPX 中 DropDownListText 属性设置为 ItemList 中缺失的值会导致相同的错误。

        就我而言,发生的事情是我错误地添加了meta-resourcekey 标记,其中Text 属性设置为ItemList 中缺少的值。

        资源值是:

        table {
          border-collapse: collapse;
          font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
        }
        table th, table td {
          border: solid 1px darkgray;
          padding: 0 5px;
        }
        
        table.tr:first(){
          
        }
        <table css="table">
          <tr>
            <th>Name</th>
            <th>Value</th>
            <th>Comment</th>
          </tr>
          <tr>
            <td>Template.Text</td>
            <td>Template</td>
            <td></td>
          </tr>
          <tr>
            <td>Template.ToolTip</td>
            <td>template of standard outgoing email settings</td>
            <td></td>
          </tr>
        </table>

        ASPX 控件是:

                <asp:DropDownList runat="server" ID="ddlTemplate" CssClass="form-control dropdownlist" meta:resourcekey="Template" >
                    <asp:ListItem Selected="True" meta:resourcekey="TemplateEmpty" Value="EMPTY" />
                    <asp:ListItem Selected="False" meta:resourcekey="TemplatePOP3" Value="POP3" />
                    <asp:ListItem Selected="False" meta:resourcekey="TemplatePOP3Secured" Value="POP3S" />
                    <asp:ListItem Selected="False" meta:resourcekey="TemplateIMAP" Value="IMAP" />
                    <asp:ListItem Selected="False" meta:resourcekey="TemplateIMAPSecured" Value="IMAPS" />
                </asp:DropDownList>
        

        删除meta:resourcekey 对我有用。

        【讨论】:

          【解决方案7】:
          #region by zia for if item not exist in dropdownlist
                              string qlf = dsEmp.Tables["tblEmp"].Rows[0]["Group"].ToString();
                              ListItem selLqli = ddlGroup.Items.FindByText(qlf.Trim());
                              if (selLqli != null)
                              {
                                  ddlGroup.ClearSelection();
                              }
                              else
                              {
                                  ddlGroup.SelectedIndex = 0;
          
                              }
                              #endregion
          

          【讨论】:

            【解决方案8】:

            我在 db 中使用 nvarchar(20) 作为数据类型,因为在 char 数据类型中存在空间,因此包含空间的文本存在差异。也可以使用 Trim()。

            【讨论】:

              【解决方案9】:

              你可以使用 Trim() 方法

              ddlname.Text = dt.Rows[0][3].ToString().Trim();
              

              【讨论】:

                猜你喜欢
                • 2015-11-02
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2011-08-07
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多