在网格视图上按下删除按钮时,必须声明标量变量"@Content"



我正在尝试在网格视图中启用删除。当您运行此代码并按删除按钮时,您会收到错误必须声明标量变量"@Content"。


代码

    <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
    <Columns>
        <asp:CommandField ShowDeleteButton="True" />
        <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
        <asp:BoundField DataField="Content" HeaderText="Content" SortExpression="Content" />
    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" 
    runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
    SelectCommand="SELECT * FROM [ImportantNews]" 
     DeleteCommand="DELETE FROM [ImportantNews] WHERE [Content] = @Content AND [Title] = @Title">
    <DeleteParameters>
        <asp:Parameter Name="Title" Type="string"/>
        <asp:Parameter Name="Content" Type="string"/>
    </DeleteParameters>
  </asp:SqlDataSource>

请注意,我使用的是 Asp.net 2.0

你可以试试这个

<asp:GridView ID="GridView2" runat="server" DataKeyNames="Content,Title"........

您需要在代码隐藏中提供参数值。

SqlDataSource1.DeleteParameters.Add("Content", "ABC");
SqlDataSource1.DeleteParameters.Add("Title", "XYZ");

我发现了问题。

Yoy 需要将网格视图控件上的 DataKeyNames 属性设置为 GradeID

检查答案 1

最新更新