【问题标题】:Binding DropDownList SelectedValue to DataValueField for GridView Update将 DropDownList SelectedValue 绑定到 DataValueField 以进行 GridView 更新
【发布时间】:2011-09-11 23:28:33
【问题描述】:

好的,我读到了这个: How to set SelectedValue of DropDownList in GridView EditTemplate

我也遇到了同样的问题。但是,我不想将选定的值绑定到显示的文本,而是绑定到值。它们是从 SQLDataSource 中选择的不同属性。这是我的 DDL 代码及其 SQLDataSource:

<asp:DropDownList ID="FoodCodeDropDownList" runat="server"
     DataSourceID="Plant_Carton_Food_List"
     DataTextField="Food_Description"
     DataValueField="PlantMaterial_FoodID"
     SelectedValue='<%# Bind("PlantMaterial_FoodID") %>' >
</asp:DropDownList>
<asp:SqlDataSource ID="Plant_Carton_Food_List" runat="server" 
     ConnectionString="<%$ ConnectionStrings:OMSConnectionString %>" 
     SelectCommand="SELECT   P.PlantMaterial_FoodID,
               M.Material_SAP_Value + ' - ' + MD.SAP_Long_Description AS Food_Description 
          FROM Plant_Carton_Food AS P 
          LEFT OUTER JOIN Material_Descriptions AS MD 
               ON P.PlantMaterial_FoodID = MD.Material_ID 
          LEFT OUTER JOIN Materials AS M 
               ON P.PlantMaterial_FoodID = M.Material_ID">
 </asp:SqlDataSource>

这是 GridView 的(删节的)SQLDataSource:

   <asp:SqlDataSource ID="Plant_Carton_Table" runat="server" 
        OldValuesParameterFormatString="old_{0}"
        ConnectionString="<%$ ConnectionStrings:DBConnectionString %>" 
        OnInserting="Plant_Carton_Food_Table_Inserting"
        OnInserted="Plant_Carton_Food_Table_Inserted"
        InsertCommand="spPlantCartonInsert" InsertCommandType="StoredProcedure" 
        SelectCommand="spPlantCartonSelect" SelectCommandType="StoredProcedure" 
        UpdateCommand="spPlantCartonUpdate" UpdateCommandType="StoredProcedure">
        <UpdateParameters>
            <asp:Parameter Name="Active_Case"           Type="Boolean" />
            <asp:Parameter Name="PlantMaterial_FoodID"  Type="String" />
            <asp:Parameter Name="PlantMaterial_CaseID"  Type="String" />
            ...
        </UpdateParameters>
        ...
    </asp:SqlDataSource>

最后,我的例外:

数据绑定:'System.Data.DataRowView' 不包含与 名称“PlantMaterial_FoodID”。

我对通过 GridView 编辑模板进行数据绑定的工作方式确实了解不多,但我能够看到正确的值通过 OnRowCommand 事件处理程序进行更新。如何将这些值传播到 SQLDataSource 而不会获得 NULL?

现在,我想任何关于 GridView 模板的数据绑定一般如何工作的解释都会有所帮助。谢谢!

【问题讨论】:

  • @naveen - 为下拉列表和 GridView 添加了数据源。
  • 和绑定gridview的数据源/表? PlantMaterial_FoodID 不会出现在那里。请也粘贴那个

标签: asp.net data-binding gridview drop-down-menu sqldatasource


【解决方案1】:

您是否检查过PlantMaterial_FoodID 的拼写是否与 SqlDataSource SelectCommand 中的完全一致?

【讨论】:

  • 是的,它们的拼写相同,谢谢@mika。我检查了 Select、Update 和 Insert 参数以及所有 GridView 引用,就拼写而言,一切似乎都是一致的。
【解决方案2】:

这并不是问题的真正答案,而是一种解决方法……但它对我有用。

我将 PlantMaterial_FoodID 作为隐藏列添加到 GridView 中,并将 DropDownList 上的 SelectedValue 绑定更改为引用此新列,如下所示。

新栏目

<asp:TemplateField HeaderText="Food ID" SortExpression="Food_ID">
    <EditItemTemplate>
        <asp:Label ID="FoodIDLabel" runat="server" Text='<%# Eval("Food_ID") %>'</asp:Label>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label ID="FoodIDLabel" runat="server" Text='<%# Bind("Food_ID") %>'</asp:Label>
    </ItemTemplate>
</asp:TemplateField>

...这是新的绑定

<asp:DropDownList ID="FoodCodeDropDownList" runat="server"
    DataSourceID="Plant_Carton_Food_List" DataTextField="Food_Description"
    DataValueField="PlantMaterial_FoodID" SelectedValue='<%# Bind("Food_ID") %>'
</asp:DropDownList>

这有效地将选定的下拉列表索引设置为正确的值。

【讨论】:

    猜你喜欢
    • 2014-05-21
    • 2010-10-24
    • 1970-01-01
    • 1970-01-01
    • 2013-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多