How to stop the page from rendering the .cs code on click of cancel button on javascript confirm box?
I have a button click event and in some IF condition I am asking the user to confirm. Now when the user clicks on OK button, I have to bind the new data in a grid. but if the user clicks on cancel button, it should not refresh the grid with new data.
Question: On click of cancel button on JavaScript confirm box, how can I stop the following code to be executed, or how can I just return/break from JavaScript?
Can anyone please help me out with this? Thanks in advance.
Markup
<asp:Button ID="btnCopy" runat="server" Text="Copy" Enabled="false" OnClick="btnCopy_Click" ValidationGroup="Copy" />
Code
public void btnCopy_Click(object sender, EventArgs e) {
if (Convert.ToInt32(hidId.Value) > 0) {
string scriptString = "<script language='JavaScript'> ";
scriptString += "if (confirm('Are you sure to proceed?') == false) {return} ";
scriptString += "</script>";
Page.RegisterStartupScript("Confirm", scriptString);
BindDataGrid();
}
}
You are mixing server and client side code in the same event, it has to be separated:
Script:
Markup:
Code-behind: