crescendo
asked on
Displaying "Are you sure?" alert before firing a server-side button
I have a "Delete Record" button. I want to give the user a chance to confirm his intentions before deleting the record, and would like to display a message box with "Are you sure?" and OK/Cancel. If the user presses OK the delete button posts back to the server, otherwise no further action is taken.
I know you can use the onclick event in hyperlinks, returning 'false' prevents the redirection. How do I do it with a server-side ASP.NET button?
I tried putting the following in the PageLoad event, but it didn't even display the message:
cmdDelete.Attributes("oncl ick") = "javascript:msgbox('Delete this record?')"
I know you can use the onclick event in hyperlinks, returning 'false' prevents the redirection. How do I do it with a server-side ASP.NET button?
I tried putting the following in the PageLoad event, but it didn't even display the message:
cmdDelete.Attributes("oncl
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
if (confirm("'Delete this record?")) {alert("Deleting..")}
else{exit;};
else{exit;};
try this:
cmdDelete.Attributes("oncl ick") = "javascript:if (confirm('Delete this record?')) {alert('Deleting..')}else{ exit;};"
cmdDelete.Attributes("oncl
assuming ur button name is "butDelete"
.vb file
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
butDelete.Attributes.Add(" Language", "javascript")
butDelete.Attributes.Add(" onclick", "return CheckDelete()")
End Sub
.aspx file
<script language="vbscript" id="Scriptfunction">
function CheckDelete()
if msgbox("Are you sure you want to delete this detail?", vbquestion + vbyesno,"MFD") = vbno then
CheckDelete = false
end if
end function
</script>
.vb file
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
butDelete.Attributes.Add("
butDelete.Attributes.Add("
End Sub
.aspx file
<script language="vbscript" id="Scriptfunction">
function CheckDelete()
if msgbox("Are you sure you want to delete this detail?", vbquestion + vbyesno,"MFD") = vbno then
CheckDelete = false
end if
end function
</script>
ASKER
Baan:
I tried both ways, but the message box is not being fired. Any ideas? Here is my code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lblMsg.Text = " "
cmdDelete.Attributes.Add(" onclick", "javascript:return confirm('Delete this record?')")
If Not Page.IsPostBack Then
I tried both ways, but the message box is not being fired. Any ideas? Here is my code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lblMsg.Text = " "
cmdDelete.Attributes.Add("
If Not Page.IsPostBack Then
ASKER
I noticed that in the articles, the button is a LinkButton, mine is a straight ASP.NET button control. Does that make a difference?
have u tried what i suggested?
ASKER
Baan
I solved it. I had "CausesValidation = True" on the button. That was inserting its own javascript into mine, and screwing it up. Works fine now.
Thanks to all.
I solved it. I had "CausesValidation = True" on the button. That was inserting its own javascript into mine, and screwing it up. Works fine now.
Thanks to all.
1. Add the following script in your .aspx page:
<script language="javascript">
function ConfirmDelete()
{
var deleteRecord = confirm("Are you sure you want to delete the selected record?");
if (deleteRecord == false)
{
return false;
}
}
</script>
2. Add the following attribute to the link button:
linkButton.Attributes.Add( "OnClick", "javascrip t: return ConfirmDelete();");
HTH, Nauman.
<script language="javascript">
function ConfirmDelete()
{
var deleteRecord = confirm("Are you sure you want to delete the selected record?");
if (deleteRecord == false)
{
return false;
}
}
</script>
2. Add the following attribute to the link button:
linkButton.Attributes.Add(
HTH, Nauman.
That shouldn't make any difference. Put a ";" at the end..
cmdDelete.Attributes.Add(" onclick", "javascript:return confirm('Delete this record?');")
~~~~ here
cmdDelete.Attributes.Add("
~~~~ here
ASKER
Hi
Yes, I can see that a semi-colon would help, however, the code that comes next is the test for client-side validation. If I needed to validate the fields before proceeding with the delete (which I don't) then it should validate *before* it asks the user. However, it's a non-issue as I'm not concerned in this case whether the user has entered any other data.
It's an interesting point, though. I can foresee many circumstances where you might want to enter data and then prompt before processing it.
Yes, I can see that a semi-colon would help, however, the code that comes next is the test for client-side validation. If I needed to validate the fields before proceeding with the delete (which I don't) then it should validate *before* it asks the user. However, it's a non-issue as I'm not concerned in this case whether the user has entered any other data.
It's an interesting point, though. I can foresee many circumstances where you might want to enter data and then prompt before processing it.
>>I had "CausesValidation = True" on the button
oh! so u had some validation controls set in the page? if s, then it would create the onlick event for the button to call the validators ...
oh! so u had some validation controls set in the page? if s, then it would create the onlick event for the button to call the validators ...
http://www.dotnetjohn.com/articles/articleid68.aspx
-Baan