I have a simple asp.net web forms page that does an insert to my sql server db. My server was running slow at the time and I pressed Insert button several times because I didn't think it took but it did all 3 t开发者_运维问答imes.
So I have duplicates from that one interaction. How would I prevent this?
Thanks, rod.
Bind the onClick of your button to a javascript and disable or hide the button and optionally display a message like "Please Wait.." to avoid multiple clicks
Code Behind:
Button.Attributes.Add("onclick", "fn_OkClick();");
ASPX file:
<script language="javascript" type="text/javascript">
function fn_OkClick()
{
var okieButton = document.getElementById('<%=Button.ClientID%>');
okieButton.style.display = 'none';
}
</script>
I'd first evaluate if the business rule is correct. Are you sure you shouldn't be able to insert multiple records with those same details? Generally I would think there would be some validation around whether or not something can be inserted. If you can do a blind insert then it suggests you should be able to do multiple inserts of the same data.
Take a resource booking program. For a given resource, at a given time, it can only be booked once. Therefore you would want to prevent people doing what you've done and inserting multiple records into the booking table for the same item at the same time.
As such there should be some validation before you try to insert the record to ensure that the selected resource is in fact available at the time you've selected. If so, insert the record, if not, don't insert the record, and return a meaningful reason to the user as to why.
If there is no validation, then there should be no reason to prevent the user from inserting multiple records.
精彩评论