Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why does refresh process another record?

Status
Not open for further replies.

tlhawkins

Programmer
Dec 28, 2000
797
US
Hello,

I've got a repeater control that lists usernames and has a few buttons to interact with the user. The one I'm concerned about is a Delete button that passes an ID as the command argument:

Code:
<asp:Button ID="DeleteRecipient" CommandArgument='<%#Container.DataItem.ID %>' Text="Big Red X" runat="server" OnClientClick="confirmDelete()"/>

Yes, it's eventually going to be an ImageButton with a big red X, but right now it just says it is.

The code behind on the repeater.ItemCommand event looks for the button id = "DeleteRecipient", gets the ID from the CommandArgument and deletes the record from the DB.
Code:
ElseIf Btn.ID = "DeleteRecipient" Then
  Dim Recip As String = e.CommandArgument.ToString()
  If Not Recip = "" Then
    Dim RecipId As Integer
    If IsNumeric(Recip) Then
      RecipId = CInt(Recip)
      ' Delete code is handled elsewhere, and is working fine
      Dim Opt As New tdb_OptInCell(RecipId)
      If Opt.DeleteRecipient(RecipId) Then 
        ActMessage.Text = "Recipient Deleted"
      Else
        ActMessage.Text = "Unable to Delete Recipient"
      End If
    End If
  End If
End If

The code works fine. If you click on the "Big Red X" button that record is deleted and the page refreshes with the message "Recipient Deleted" and the record is gone from the list.

The problem comes when you refresh the page. It asks if you want to resubmit the form, you know someone will do it so I have to test. In that case it deletes the NEXT record on the list. It should be passing the ID of that record in and the delete should fail, or at least do nothing.
Why is it deleting the next record. If you keep refreshing and resubmitting the form it will keep deleting the next record until all are gone.

Please help
Thanks

Travis Hawkins
jobs.bestcodingpractices.com
 
the issue with refresh is webforms and the posting of the form. when you refresh after clicking the button you are re-submitting the form. that last action of the form was to delete the record. so it's preforming another delete.

the next record is deleted because webforms is using the index of the row/item to figure out which button/value/command to use. for example:
[tt]
row 1 item a [index 0]
row 2 item b [index 1]
row 3 item c [index 2]
row 4 item d [index 3]
[/tt]
click delete for item b (row 2, index 1)
delete row at index 1
reload data
[tt]
row 1 item a [index 0]
row 2 item c [index 1]
row 3 item d [index 2]
[/tt]
refresh - submits delete row 2 index 1
at this point item c is located at index 1 so it's deleted

I would imagine an index out of range exception would be thrown if you delete the last item and then refresh the page.

I'm not sure if/how to get around this with webforms. the user shouldn't be refreshing the page manually anyway.

with web applications that do not use webforms you can resolve this issue using the post-redirect-get approach. post to the server to issue a command to change data, redirect from that operation to issue a query to present data to the user. this uses GET instead of POST. then if the user refreshes the page manually the last request issued was a GET to query data, not the POST to alter the data.

this starts to scratch the surface of the concept "command query separation" in the context of http. a request should either issue a query (select data to display) or a command (change the data) not both. and webforms is issuing both a command and a query in the same request.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Silly me thinking that you tell it to use the ID for the CommandArgument and it actually would.
Thanks for explaining what's going on.
I guess from a UI perspective, based on the "command query separation" principle, it should really pull up the record and ask if the user wants to delete it. After delete it redirects back to the list page. Then a refresh would just refresh the list, and a Back button would only have 1 record to possibly delete.


Travis Hawkins
jobs.bestcodingpractices.com
 
with post-redirect-get the back button would take you back to the list (query) that was the last request issued from the browser. the redirect happened on the server. this negates the "resend form" popup.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top