Dear Friends,
I need to display an alert box (OK/Cancel) when the user clicks the Delete Button. It should asks ‘Are you sure you want to cancel? The data will be lost!’. If the User clicks the OK Button, the records need to be deleted from the database and the page should
be redirected to the login page. If the user clicks CANCEL button,it should remain in the same page. Below shown is my code.
Private Function ReturnValue() As Boolean
Return False
End Function
Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
Dim CSM As ClientScriptManager = Page.ClientScript
If Not ReturnValue() Then
Dim strconfirm As String = "<script>if(window.confirm(‘Are you sure you want to cancel? The data will be lost!’)){window.location.href=’login.aspx’; }</script>"
CSM.RegisterClientScriptBlock(Me.[GetType](), "Confirm", strconfirm, False)
End If
End sub
Now, When i click the OK Button,its redirected to the Login.aspx page. But i need to delete the records before going to that page. How can i add that code here. Please some one help me. Its really urgent.
Thanks in advance.
Rather than going to login.aspx, go to DeleteThenLogin.aspx and on that page do your deletes then
Response.Redirect("Login.aspx")
Dear Friend,
Can you please show me where to write the code for deletion inside this code.
Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
Dim CSM As ClientScriptManager = Page.ClientScript
If Not ReturnValue() Then
Dim strconfirm As String = "<script>if(window.confirm(‘Are you sure you want to cancel? The data will be lost!’)){window.location.href=’login.aspx’; }</script>"
CSM.RegisterClientScriptBlock(Me.[GetType](), "Confirm", strconfirm, False)
End If
End sub
Thanks.
in aspx
<asp:button ID="btnDelete" runat="server" text="Delete" OnClientClick="return confirm('Are you sure you want to cancel? The data will be lost!')" OnClick="btnDelete_Click" />
in aspx.vb
Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click ...Code to delete HERE ' Code to redirect Response.Redirect("Login.aspx") End Sub
Hope this helps.
Thanks a lot..its working fine.
Do u have any jquery code to create an alert box with YES/NO instead of OK/Cancel? If you have, Please post it.
Bye.
It’s more complex but can be done using following code.
Instead of OnClientClick="return confirm(…)"
1) Create a dialog
<div id="dialog-confirm" title="Dialog"> Are you sure you want to cancel? The data will be lost! </div>
2) and a script which will initiate it
<script> var yesno = function () { $("#dialog-confirm").dialog({ resizable: false, modal: true, buttons: { OK: function () { <%=Me.Page.ClientScript.GetPostBackEventReference(New PostBackOptions(Me.btnDelete))%>; }, Cancel: function () { $(this).dialog("close"); } } }); }; </script>
3) add required references to jquery and styles
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.css" /> <script src="//code.jquery.com/jquery-1.10.2.js"></script> <script src="//code.jquery.com/ui/1.11.1/jquery-ui.js"></script> <style> #dialog-confirm { display: none; } </style>
4) and finally change button to
<asp:button ID="btnDelete" runat="server" text="Button" OnClientClick="yesno();return false;" OnClick="btnDelete_Click" />
Hope this helps.