执行期间何时将DataBound GridView字段绑定到SQL参数?我有一些与它们相关的DataBound字段和UpdateParameters。有时,我的UpdateCommand将成功执行,而其他时候,我会得到空引用错误。
这是我的代码:
数据源
<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();
}
最佳答案
我认为您不必包含Cell[0]
。此外,GridViewUpdateEventArgs
具有可以使用的RowIndex
属性。
这应该工作
DropDownList ddlReconcileStatus = (DropDownList)reconcileGrid.Rows[e.RowIndex].FindControl("ddlReconcileStatus");
DropDownList ddlTransferLocation = (DropDownList)reconcileGrid.Rows[e.RowIndex].FindControl("ddlTransferLocation");
关于c# - GridView DataBound字段-SQL参数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6169337/