开发者

Binding DropDownList SelectedValue to DataValueField for GridView Update

开发者 https://www.devze.com 2023-03-12 00:11 出处:网络
开发者_JS百科Ok guys, so I read this: How to set SelectedValue of DropDownList in GridView EditTemplate

开发者_JS百科Ok guys, so I read this: How to set SelectedValue of DropDownList in GridView EditTemplate

and I'm having the same issue. However, I don't want to bind the selected value to the displayed text, but instead the values. They are different attributes selected from a SQLDataSource. Here is my DDL code with its 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>

Here is the (abridged) SQLDataSource of the GridView:

   <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>

And, finally, my exception:

DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'PlantMaterial_FoodID'.

I really don't have much knowledge on how databinding through the GridView Edit templates work, but I was able to see that the correct values pass through the OnRowCommand event handler for updates. How do I propagate these values to the SQLDataSource without getting NULL?

Right now, I guess any explanation on how databinding with GridView templates work in general would be beneficial as well. Thanks!


This isn't really an answer to the question, but instead a workaround... but it works for me.

I added PlantMaterial_FoodID as a hidden column into the GridView and changed the SelectedValue binding on the DropDownList to reference this new column, as shown below.

New Column

<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>

...and here is the new binding

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

This effectively sets the selected dropdownlist index to the correct value.


Have you checked that PlantMaterial_FoodID is spelled exactly as in the SqlDataSource SelectCommand?

0

精彩评论

暂无评论...
验证码 换一张
取 消