【发布时间】: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