Asp.net 在 GridView.AllowPaging="true" 时生成错误的 SQL 请求

Asp.net generates a wrong SQL request when GridView.AllowPaging="true"

这是基本代码

<asp:SqlDataSource runat="server"
    ID="StuffDataSource"
    SelectCommand="select employeeID,lastName, firstName, titleOfCourtesy, birthDate, hireDate, address, city, country, homePhone from Employees"
    UpdateCommand="update Employees set lastName=@lastName, firstName=@firstName, address=@address, city=@city, country=@country, homePhone=@homePhone where employeeID=@employeeID"
    DeleteCommand="delete from Employees where employeeID=@employeeID" 
    ConnectionString="<%$ connectionStrings:NorthwindSqlString %>"
    FilterExpression="(lastName like '%{0}%' or firstName like '%{0}%') and country like '%{1}%'"
    OnFiltering="StuffDataSource_Filtering">
    <FilterParameters>
        <asp:ControlParameter Name="Name" ControlId="keyword" PropertyName="Value" DefaultValue="%"/>
        <asp:ControlParameter Name="Country" ControlId="country" PropertyName="Value" DefaultValue="%"/>
    </FilterParameters>
</asp:SqlDataSource>

<asp:GridView runat="server" 
    ID="StuffGridView" 
    DataSourceID="StuffDataSource" 
    OnRowCommand="StufGridView_RowCommand"
    DataKeyNames="employeeID"
    AutoGenerateColumns="false" 
    AllowPaging="true" PageSize="20">
    <Columns>
        <asp:TemplateField HeaderText="序号">
            <ItemTemplate>
                <asp:Label runat="server" Text='<%# (Container.DataItemIndex + 1).ToString() %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="employeeID" HeaderText="id" Visible="false" />
        <asp:BoundField DataField="lastName" HeaderText="名" />
        <asp:BoundField DataField="firstName" HeaderText="姓" />
        <asp:BoundField DataField="titleOfCourtesy" ReadOnly="true" HeaderText="性别" />
        <asp:BoundField DataField="birthDate" HeaderText="出生日期" ReadOnly="true" Visible="false" />
        <asp:BoundField DataField="hireDate" HeaderText="雇用日期" ReadOnly="true" />
        <asp:BoundField DataField="address" HeaderText="地址" />
        <asp:BoundField DataField="city" HeaderText="城市" />
        <asp:BoundField DataField="country" HeaderText="国籍" />
        <asp:BoundField DataField="homePhone" HeaderText="联系" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Button runat="server" ID="Edit" Text="编辑" CommandName="Edit" CommandArgument='<%# Container.DataItemIndex.ToString() %>' Visible='<%# StuffGridView.EditIndex != Container.DataItemIndex ? true : false %>' />
                <asp:Button runat="server" ID="Delete" Text="删除" CommandName="RequestDelete" CommandArgument='<%# Container.DataItemIndex.ToString() + "," + Eval("firstName").ToString() + " " + Eval("lastName").ToString() %>' Visible='<%# StuffGridView.EditIndex != Container.DataItemIndex ? true : false %>' />
                <asp:Button runat="server" ID="Update" Text="更新" CommandName="Update" CommandArgument='<%# Container.DataItemIndex.ToString() %>' Visible='<%# StuffGridView.EditIndex == Container.DataItemIndex ? true : false %>' />
                <asp:Button runat="server" ID="Cancel" Text="取消" CommandName="Cancel" CommandArgument='<%# Container.DataItemIndex.ToString() %>' Visible='<%# StuffGridView.EditIndex == Container.DataItemIndex ? true : false %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

让我们关注 SqlDataSource.DeleteCommandGridView.AllowPaging

GridView.AllowPaging="false"时,我可以正常删除记录。但是当我设置 GridView.AllowPaging="true" 时,我不能删除记录,除非记录在第一页上,并且从 SQL SERVER

得到这个错误

Must declare the scalar variable “@employeeID” in delete statement

但我想一切都准备好了。有 DataKeyNames 设置为 employeeID,一个 BoundField 包括一个 DataField 设置为 employeeID。如果有一些错误,为什么当AllowPaging设置为false时它会起作用?

所以我想知道 SQL SERVER 实际收到了哪些请求。 我在 SQL SERVER management studio 上启动了 Profiler。结果如下:

AllowPaging="false" -> 删除任何页面上的记录

AllowPaging="true" -> 删除第一页记录

exec sp_executesql N'delete from Employees where employeeID=@employeeID',N'@employeeID int',@employeeID=37

AllowPaging="true" -> 删除除第一页以外的任何页面上的记录

delete from Employees where employeeID=@employeeID

那么为什么Asp.Net在AllowPaging = "true"时无法生成正确的SQL请求,并且删除除第一页以外的任何页面的记录?我看不懂,如何避免这个问题?

这一行:

<asp:Button runat="server" ID="Delete" Text="删除" CommandName="RequestDelete" CommandArgument='<%# Container.DataItemIndex.ToString() + "," + Eval("firstName").ToString() + " " + Eval("lastName").ToString() %>' Visible='<%# StuffGridView.EditIndex != Container.DataItemIndex ? true : false %>' />

问题可能是由于 DataItemIndex。根据 DataTable 不知道您在哪个页面。所以你必须通过偏移公式设置合适的索引。

尝试这样的事情:

<asp:Button runat="server" ID="Delete" Text="删除" CommandName="RequestDelete" CommandArgument='<%# Container.DataItemIndex.ToString() + ((StuffGridView.ActivePage - 1) * StuffGridView.PageSize) + "," + Eval("firstName").ToString() + " " + Eval("lastName").ToString() %>' />