views:

666

answers:

3

I have a Repeater that has a gridview in it, which uses an SqlDataSource with a delete command. When I click delete on a given row, it posts back but doesn't fire the delete. Am I forgetting something?

+2  A: 

Have you set the GridView's DataKeyNames property? It needs to be set to the item's primary key to uniquely identify the record to modify. If you didn't set it, clicking Delete (or Edit) would cause a postback that doesn't affect anything since a DataKey wasn't associated with each GridView row.

According to the GridView.DataKeys property page:

When the DataKeyNames property is set, the GridView control automatically creates a DataKey object for each row in the control. The DataKey object contains the values of the field or fields specified in the DataKeyNames property.

For example, let's say you're deleting a product item that's identified by a ProductID field.

Your SqlDataSource might look something like this:

<asp:SqlDataSource ID="ProductsDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:NORTHWNDConnectionString %>"
SelectCommand= "SELECT [ProductID], [ProductName], [UnitPrice] FROM [Products]" 
DeleteCommand="DELETE FROM Products WHERE ProductID = @ProductID">
    <DeleteParameters>
      <asp:Parameter Name="ProductID" />
    </DeleteParameters>
</asp:SqlDataSource>

You would then assign the same "ProductID" primary key to the GridView's DataKeyNames property:

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1"
        DataKeyNames="ProductID" ...

These tutorials may be helpful to you:

Ahmad Mageed
Yes, I successfully use this gridview setup all over my app, and I do specify the DataKeyNames. The only difference between the tens of other gridviews and this one is that it is the child of a repeater.
Pete Michaud
+1  A: 

First of all,you should add some code for clear questions.Anyone who will try to help you will ask you for some code.

In my opinion, *if you are using Button,LinkButton,Image for delete operations may be you forgot using CommandName

CommandField which is declared in GridView Columns:

<asp:CommandField ShowDeleteButton="True" />

TemplateField might be:

<asp:TemplateField>
   <ItemTemplate>
      <asp:LinkButton ID="lnkDelete" runat="server" CommandName="Delete" Text="Delete"/>
   </ItemTemplate>
</asp:TemplateField>

Above declarations will have same result when clicking the link.Check that you have successfully assigned CommandName property.

*Other opinion if you look more in details OnRowDeleting event,if are you setting

e.Cancel = true //remove it

*Also are you using UpdatePanel between postback operations ??

Myra
+1  A: 

I just set up a very basic example with no problems. Without posting code (as many others have asked for) there isn't much we can help you with.

Here is the code from my example - nothing tricky here, just bound a string array to a repeater, and bound the gridview to a sqldatasource with the select and delete commands specified.

All I can think is that your datasource doesn't have the delete command / method specified (but you'd get a clear error about this), or that you don't have the DataKeyNames specified in the gridview (but you'd get a clear error about that as well).

<asp:Repeater ID="rp" runat="server">
<ItemTemplate>
<p>Row: <%#Container.DataItem%></p>

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" DataKeyNames="CroppingID">
    <Columns>
        <asp:CommandField ShowDeleteButton="True" />
        <asp:BoundField DataField="CroppingID" HeaderText="CroppingID" ReadOnly="True" SortExpression="CroppingID" />
        <asp:BoundField DataField="CroppingName" HeaderText="CroppingName" SortExpression="CroppingName" />
    </Columns>
</asp:GridView>

</ItemTemplate>
</asp:Repeater>

and the datasource (from an existing database I have set up)

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:sConn %>" DeleteCommand="spD_Cropping" 
    DeleteCommandType="StoredProcedure" SelectCommand="spS_Croppings" 
    SelectCommandType="StoredProcedure">
    <DeleteParameters>
        <asp:Parameter Name="CroppingID" Type="Byte" />
    </DeleteParameters>
</asp:SqlDataSource>
ScottE