开发者

Deleting from ASP.NET Repeater item - erroneous deleting...?

I have a repeater which binds a set of data. Within this repeater is a column with various controls for updating, deleting, etc. These are image buttons which fire an onclick event such as "DeleteRecord". All this does is fire a stored procedure, passing in the ID of the record to delete from the CommandArgument of the object.

This works wonderfully... except for one rather huge problem. Once you delete a record, if you refresh the page, the record where the first deleted record used to be gets deleted.

For instance... if I have 4 records

1 Record1
2 Record2
3 Record3
4 Record4

and I delete record 2... The page reloads with (which is fine):

1 Record1
3 Record3
4 Record4

...if I then hit refresh...

1 Record1
4 Record4

I assume this is because the erroneously deleted object (record3) is now in the same hierarchical place as the old object used to be and .net therefore doesn't know the difference, the page refreshes and fires the onlick event,开发者_如何学Python grabbing out the command argument of the new object and deletes based on the ID as obtained from the commandargument of the new object.

This is obviously a huge problem, if a client did this it would destroy data erroneously and I'm at a loss here.

Is there any way to stop this from happening? I'm not sure if there is a better way to go about doing things or not. If there isn't, I need some sort of way to tell the page not to execute the event or to cross reference the ID of the object that is intended for deletion against the object itself...

Code below for convenience...

EDIT Wrapped a LinkButton around it because I have some jquery code in here as well which stops the page execution to wait for user confirmation. Pressing "ok" continues page execution.

<asp:LinkButton ID="oDeleteLink" CssClass="oDeleteIcon" CommandName="Delete" CommandArgument='<%# Eval("iAccountID") %>' runat="server">
     <asp:ImageButton ImageUrl="/files/system/icons/trash-steel-16.png" ToolTip="Delete This Account" AlternateText="Delete" ID="oDeleteIcon" runat="server" />
</asp:LinkButton>

    protected void oAccounts_ItemCommand(Object Sender, RepeaterCommandEventArgs e) {
        if (e.CommandName == "Delete") {
            int ID = e.CommandArgument.ToString().Numeric();
            db.SPs.SpDeleteAccount(ID).Execute();
            UI.Confirm(uiBroadcast, "Account has been deleted", "300px");
            BindAccounts();
        }
    }

Would appreciate any feedback you folks could give.


Use ItemCommand event and CommandName/CommandArgument instead:

<!-- CommandArgument binding would be the same as whatever you are binding your ID label text value to -->
<asp:ImageButton 
    CommandName="Delete" CommandArgument='<%# Eval("ID") %>'
    ImageUrl="/files/system/icons/trash-steel-16.png"  
    ToolTip="Delete This Account" 
    AlternateText="Delete" CssClass="oDeleteIcon"  
    ID="oDeleteIcon" runat="server" /> 

And the event handler:

void Repeater_ItemCommand(Object Sender, RepeaterCommandEventArgs e) {        
    if( e.CommandName == "Delete" ) {
        int id = e.CommandArgument.ToString().Numeric();

        db.SPs.SpDeleteAccount(id).Execute();
        // the rest of your code
    }
} 

This way the delete button itself is indicating what ID to delete, as opposed to relying on positional elements in your repeater.


You need to follow the Post-Redirect-Get pattern [1]

Basically show the data, take the command request and redirect back to show the data, do not show the update in response to the post. Any refresh will re-request the data not re-perform the post

adam

[1] http://en.wikipedia.org/wiki/Post/Redirect/Get

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜