Jorg Ancrath
Jorg Ancrath

Reputation: 1447

Deleting row from Repeater

I have the following Repeater on my page:

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1" 
        onitemcommand="Repeater1_ItemCommand">
        <ItemTemplate>
            <div id="fullcommentheader">
                <span class="fullname">
                    <asp:Literal ID="Literal3" runat="server" Text='<%# Eval("Name") %>'></asp:Literal></span>
                <br />
                <span class="fullbodytext">
                    <asp:Button ID="Button2" runat="server" CommandName="Delete" CommandArgument='<%# Eval("Id") %>' Text="Delete" />
                    <asp:Literal ID="LitBody2" Text='<%# Eval("Message")%>' runat="server"></asp:Literal></span>
                <span class="dateTime">
                    <asp:Literal ID="Literal4" runat="server" Text='<%# Eval("CreateDateTime") %>'></asp:Literal></span>
            </div>
            <br />
        </ItemTemplate>
    </asp:Repeater>

I have a Button2 there that I'd like to use to delete the repeater entry, how do I query the database to achieve this? I'm used to have these commands by default on gridview, so I'm unsure on how to do this manually.

My event:

protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "Delete" && e.CommandArgument.ToString() != "")
        {

        }
    }

This is my SqlDataSource:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:orangefreshConnectionString1 %>"
        DeleteCommand="DELETE FROM [Comment] WHERE [Id] = @Id" 
        InsertCommand="INSERT INTO [Comment] ([Name], [Email], [Website], [Message], [PostId]) VALUES (@Name, @Email, @Website, @Message, @PostId)"
        SelectCommand="SELECT [Name], [Email], [Website], [Message], [PostId], [Id], [CreateDateTime] FROM [Comment] WHERE ([PostId] = @PostId)"
        UpdateCommand="UPDATE [Comment] SET [Name] = @Name, [Email] = @Email, [Website] = @Website, [Message] = @Message, [PostId] = @PostId, [CreateDateTime] = @CreateDateTime WHERE [Id] = @Id">
        <DeleteParameters>
            <asp:Parameter Name="Id" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="Name" Type="String" />
            <asp:Parameter Name="Email" Type="String" />
            <asp:Parameter Name="Website" Type="String" />
            <asp:Parameter Name="Message" Type="String" />
            <asp:QueryStringParameter Name="PostId" QueryStringField="Id" Type="Int32" />
            <asp:Parameter Name="CreateDateTime" Type="DateTime" />
        </InsertParameters>
        <SelectParameters>
            <asp:QueryStringParameter Name="PostId" QueryStringField="Id" Type="Int32" />
        </SelectParameters>
        <UpdateParameters>
            <asp:Parameter Name="Name" Type="String" />
            <asp:Parameter Name="Email" Type="String" />
            <asp:Parameter Name="Website" Type="String" />
            <asp:Parameter Name="Message" Type="String" />
            <asp:Parameter Name="PostId" Type="Int32" />
            <asp:Parameter Name="CreateDateTime" Type="DateTime" />
            <asp:Parameter Name="Id" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>

Upvotes: 0

Views: 5986

Answers (2)

Steve
Steve

Reputation: 399

You will need to declare a private int _id global variable first.

Then, add the parameter dynamically in your sqldatasource deleting event:

Then in your ItemCommand 'delete', set the global _id to the CommandArgument since you passed that in. Then perform a SqlDataSource1.Delete()

_id = Convert.ToInt32(e.CommandArgument);
SqlDataSource1.Delete();
_id = 0;
//need to rebind your repeater here or you won't see the changes


protected void SqlDataSource1_Deleting(object sender, SqlDataSourceCommandEventArgs e)
{
  //add the parameter
  if (_id != 0)
    e.Command.Parameters["@Id"].Value = _id;
}   

Upvotes: 1

DCNYAM
DCNYAM

Reputation: 12126

Use the ItemCommand event for the repeater. This will trigger when the button is clicked and give you access to the command name and command argument. You can then use the command argument to delete the record from the database.

Do delete the record, you can use the SqlDataSource.Delete() method, but you will need to populate the delete parameter before calling that method or handle the Deleting event and populate the parameters there.

Upvotes: 0

Related Questions