views:

204

answers:

3

I write this code for delete from repeater,but I have a problem. when I run my page ad delete one of rows , this row is deleted and when I refresh that page , another one rows deleted that I don't want delete that.in fact I want to delete one row but delete two row when I refresh page

protected void SendBoxrep_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    MembershipUser Admin = Membership.GetUser(User.Identity.Name);

    if (e.CommandName == "del")
    {
        Guid g = new Guid(e.CommandArgument.ToString());
        MessageClass.deleteMessage(g);

        SendBoxrep.DataSource = MessageClass.selectMessagesUser(Admin);
        SendBoxrep.DataBind();
    }

}

    public static void deleteMessage(Guid id)
{
    foreach (Message item in MessageClass.LoadAll(id))
    {
        MDB.Messages.DeleteOnSubmit(item);

        MDB.SubmitChanges();
    }
}
A: 

Comments above show you're refreshing your page via F5; this is known to cause problems on ASP.NET pages because of how they post back, an how their lifecycle works. I recommend instead creating a Refresh button on the page itself, that does the postback and updates the necessary information the Repeater is bound to.

Adam Maras
A: 

In a scenario like this, I've had success using Response.Redirect to reload the page and get rid of the postback information after the postback event has been handled. This way the postback will happen only once and refreshing the page using the browser shouldn't cause any problems. Instead of calling the Repeater.DataBind...

Response.Redirect(Request.RawUrl);

You may have to make design changes to other parts of your page or add a parameter to the query string indicating that you are reloading the page, but that's the tax of providing this ability.

Tom
A: 

Try to delete based on primary key of the table u r deleting.

Suppose u r deleting a table say Messages which has messageID as primary key. Now if u want to delete a particular message then send the messageID as commandArgument and delete that.

After delete when you r refreshing the page the same event occurs i.e. if you press a delete button in a row to delete the message the event SendBoxrep_ItemCommand fired and taking the commandArgument it deletes the record. Again you press F5 to refresh the page then the previous event for delete is fired. So your two records are being deleted.

Now using primary key (messageID) it will delete only one record even if you fire the same event twice by pressing F5.

Himadri