【问题标题】:Dropdownlist in DetailsView详细信息视图中的下拉列表
【发布时间】:2011-06-30 14:24:00
【问题描述】:

我已经向 DetailsView 添加了一个 DropDownList 控件,并且我试图让它绑定到我的更新/插入方法的“状态”参数。

<InsertItemTemplate>
<asp:DropDownList ID="States" runat="server" SelectedValue='<%# Bind("State") %>'>
<asp:ListItem Value="NY">New York</asp:ListItem>
....
</asp:DropDownList>
</InsertItemTemplate>

我已经用 50 个状态填充了 DropDownList,因此它们没有绑定到任何表并且是静态项。我只是希望能够在编辑时选择一个状态,当我点击编辑时,它应该运行存储过程进行编辑并将 DropDownList 的值作为@State 的参数。

很遗憾,我收到了这个错误:

'States' has a SelectedValue which is invalid because it does not exist in the 
list of items.
Parameter name: value

我研究了这个问题,但一些帖子建议使用“Eval”而不是“Bind”或添加一些空值,但这并没有解决我的问题。你们对我如何摆脱这个错误有任何想法/建议吗?

【问题讨论】:

  • when I hit edit 你不应该看EditTemplate 吗?
  • 是的,我的意思是在你填写EditTemplate之后,然后你点击更新按钮,它应该会更新。

标签: c# asp.net


【解决方案1】:

如果 ListItems 是静态的(即在标记中定义),我相信您也应该发布它们。 请记住 ListItems 的正确语法是

<asp:ListItem Value="value" Text="displayText"></asp:ListItem>

【讨论】:

  • 我用一个列表项更新了 OP。所有其他项目具有相同的结构;只是不同的文字。我相信格式也是正确的吧?
  • 检查您的数据源中是否有寄存器具有未在 ListItems 中列出的“状态”值。
【解决方案2】:

如果数据库表中的某些值与您的 listitem 值不匹配,则会出现此错误。所以请检查您的表条目

【讨论】:

    【解决方案3】:

    将其绑定到项目模板值,例如

     <ItemTemplate>
      <asp:Label ID="lbl1" runat="server" Text='<%# Bind("State") %>'></asp:Label>
    </ItemTemplate>
    

    那么你可以:

    InsertItemTemplate>
    <asp:DropDownList ID="States" runat="server" SelectedValue='<%# Bind("State") %>'>
    <asp:ListItem Value="NY">New York</asp:ListItem>
    ....
    </asp:DropDownList>
    </InsertItemTemplate>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-10
      • 1970-01-01
      相关资源
      最近更新 更多