GridView数据绑定字段-SQL参数

本文关键字:参数 -SQL 字段 数据绑定 GridView | 更新日期: 2023-09-27 17:58:07

执行期间,DataBound GridView字段何时绑定到SQL参数?我有一些DataBound字段和与它们相关的UpdateParameters。有时我的UpdateCommand会成功执行,而其他时候我会得到null引用错误。

这是我的代码:

数据源

<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();
    }

GridView数据绑定字段-SQL参数

我认为您不必包含Cell[0]。此外,GridViewUpdateEventArgs还有一个可以使用的RowIndex属性。

这应该工作

DropDownList ddlReconcileStatus = (DropDownList)reconcileGrid.Rows[e.RowIndex].FindControl("ddlReconcileStatus");
DropDownList ddlTransferLocation = (DropDownList)reconcileGrid.Rows[e.RowIndex].FindControl("ddlTransferLocation");