在执行期间,DataBound GridView字段绑定到SQL参数?我有一些与它们相关的DataBound字段和UpdateParameters。有时我的UpdateCommand会成功执行,有时我会得到null引用错误。
这是我的代码:
DATA SOURCE
<asp:SqlDataSource ID="reconcileDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="GetReconcileItems" SelectCommandType="StoredProcedure"
UpdateCommand=" UPDATE dbo.item SET Stat=@Status WHERE ItemID = @ID;
INSERT INTO dbo.transactions(ItemID, TransactionTypeID, LocationID, DestinationID, UserNote)VALUES(@ID, '14', @LocationID, @TransferLocation, 'TEST');" >
<UpdateParameters>
<asp:Parameter Name="ID" />
<asp:Parameter Name="Status" />
<asp:Parameter Name="LocationID" />
<asp:Parameter Name="TransferLocation" />
</UpdateParameters>
</asp:SqlDataSource>
ASP.NET
<asp:UpdatePanel ID="reconcileUpdatePanel" runat="server">
<ContentTemplate>
<asp:GridView ID="reconcileGrid" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" DataSourceID="reconcileDataSource" OnRowUpdating="reconcileGrid_Updating" >
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" ReadOnly="true" />
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" ReadOnly="true"/>
<asp:BoundField DataField="last_known_location" HeaderText="Last Known Location" SortExpression="last_known_location" ReadOnly="true" />
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label ID="lblStatus" runat="server" Text='<%# evalStatus(Eval("Stat")) %>'></asp:Label></ItemTemplate><EditItemTemplate>
<asp:DropDownList ID="ddlReconcileStatus" runat="server" OnSelectedIndexChanged="ddlReconcileStatus_SelectedIndexChanged" AutoPostBack="true" >
<asp:ListItem Value="3" Text="Allocated"></asp:ListItem><asp:ListItem Value="4" Text="Transferred"></asp:ListItem></asp:DropDownList></EditItemTemplate></asp:TemplateField><asp:TemplateField HeaderText="Transfer Location">
<EditItemTemplate>
<asp:DropDownList ID="ddlTransferLocation" runat="server"
DataSourceID="ddlTransferLocationDataSource" DataTextField="Name"
DataValueField="ID" Enabled="false" ></asp:DropDownList>
<asp:SqlDataSource
ID="ddlTransferLocationDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [ID], [Name] FROM [TransferLocation]">
</asp:SqlDataSource>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
C#
protected void reconcileGrid_Updating(object sender, GridViewUpdateEventArgs e)
{
DropDownList ddlReconcileStatus = (DropDownList)reconcileGrid.Rows[reconcileGrid.EditIndex].Cells[0].FindControl("ddlReconcileStatus");
DropDownList ddlTransferLocation = (DropDownList)reconcileGrid.Rows[reconcileGrid.EditIndex].Cells[0].FindControl("ddlTransferLocation");
reconcileDataSource.UpdateParameters["Status"].DefaultValue= ddlReconcileStatus.SelectedValue;
reconcileDataSource.UpdateParameters["TransferLocation"].DefaultValue = ddlTransferLocation.SelectedValue.ToString();
}
答案 0 :(得分:0)
我认为您不必包含Cell[0]
。此外,GridViewUpdateEventArgs
还有一个RowIndex
属性供您使用。
这应该有效
DropDownList ddlReconcileStatus = (DropDownList)reconcileGrid.Rows[e.RowIndex].FindControl("ddlReconcileStatus");
DropDownList ddlTransferLocation = (DropDownList)reconcileGrid.Rows[e.RowIndex].FindControl("ddlTransferLocation");