这是基本代码

<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中得到此错误。


  必须在删除语句中声明标量变量“ @employeeID”


但是我认为一切都已经准备好了。有DataKeyNames设置为employeeIDBoundField包括DataField设置为employeeID。并且,如果存在一些错误,为什么AllowPaging设置为false时为什么还能工作?

所以我想知道SQL SERVER实际收到什么请求。
我在SQL SERVER管理工作室上启动了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


那么,为什么AllowPaging = "true"并在除第一页以外的任何页中删除记录时,Asp.Net为什么不能生成正确的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引起的。根据thisDataTable不知道您在哪一页上。因此,您必须通过偏移量公式设置适当的索引。

尝试这样的事情:

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

09-25 21:57