Gridview删除不使用DeleteParameter

时间:2013-06-12 16:37:16

标签: asp.net gridview sqldatasource

我遇到Gridview删除无法正常工作的问题。它似乎与DeleteCommand和DeleteParameters有关。例如,如果我向DeleteCommand提供静态值,而不是使用DeleteParameter,则gridview会删除具有静态指定值的记录。代码如下。

<asp:GridView ID="testGridView" runat="server" AllowSorting="True" 
        AutoGenerateColumns="False" DataKeyNames="Test_ID" 
        DataSourceID="dsourceTest">
    <Columns>
            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
            <asp:BoundField DataField="Test_ID" HeaderText="Test_ID" 
            InsertVisible="False" ReadOnly="True" SortExpression="Test_ID" />
            ...MORE COLUMNS HERE...
    </Columns>
    </asp:GridView>

    <asp:SqlDataSource ID="dsourceTest" runat="server" 
        ConflictDetection="CompareAllValues" 
        ConnectionString="<%$ ConnectionStrings:TestConnectionString %>" 
        SelectCommand="SELECT Test_ID, C2, C3, C4, C5, C6, COALESCE(C7, '') AS C7, C8 FROM TestTable1" 
        DeleteCommand="DELETE FROM [TestTable1] WHERE [Test_ID] = @Test_ID">
        <DeleteParameters>
            <asp:Parameter Name="Test_ID" Type="Int32" />
        </DeleteParameters>
    </asp:SqlDataSource>

因此,使用上面的DeleteCommand,删除将不起作用。但是,如果我将其更改为DeleteCommand="DELETE FROM [TestTable1] WHERE [Test_ID] = 27",并且我在Gridview中点击了一个删除按钮,它将删除Test_ID为27的记录。

1 个答案:

答案 0 :(得分:0)

我弄清楚了......我必须将DeleteCommand更改为DeleteCommand="DELETE FROM [ClientService_AP] WHERE [Action_Plan_ID] = @original_Test_ID",因为我在SqlDataSource中也有语句OldValuesParameterFormatString="original_{0}"