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

标签 c# asp.net gridview

这是基本代码

<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 引起的。根据this , 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() %>' />

关于c# - Asp.net 在 GridView.AllowPaging ="true"时生成错误的 SQL 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56642263/

相关文章:

c# - Redis 与应用内缓存

asp.net - 在asp.net mvc中是否可以制作通用 Controller ?

ASP.NET MVC4 - OutputCache 异常

.net - 何时使用 WCF 服务ASP.Net 页面?

c# - 如何获取 Gridview 中选定行的 HTML 部分?

c# - Entity Framework SaveChanges 被调用但它不工作

c# - 如何调用异步 Task<bool> 方法?

C# 正则表达式用于查找文本中的特定模式

c# - GridView 中的数据绑定(bind)表达式

c# - 从 Javascript 更改时,HiddenField.ValueChanged 事件未触发