Clicking Cancel in the Javascript confirmation window does not stop the grid from updating

How to stop a page from rendering .cs code when clicking cancel button in javascript confirmation window?

I have a button click event and in some IF state I ask the user to confirm. Now, when the user clicks the OK button, I need to bind the new data in the grid. but if the user presses the cancel button, he should not update the grid with new data.

Question: clicking the cancel button in the JavaScript confirmation window, how can I stop the following code from executing, or how can I just return / cancel JavaScript?

Can anyone help me with this? Thanks in advance.

Markup

 <asp:Button ID="btnCopy" runat="server" Text="Copy" Enabled="false" OnClick="btnCopy_Click" ValidationGroup="Copy" /> 

the 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(); } } 
+6
source share
5 answers

You mix server and client code in the same event, you need to separate it:

Script:

 function Confirm() { var record=confirm('Are you sure to proceed??'); if(record == 1) { return true; } else if(record == 0) { return false; } } 

Markup:

 <asp:Button ID="btnCopy" runat="server" Text="Copy" Enabled="false" OnClick="btnCopy_Click" OnClientClick='return Confirm();' ValidationGroup="Copy" /> 

Code for:

 public void btnCopy_Click(object sender, EventArgs e) { if (Convert.ToInt32(hidId.Value) > 0) { BindDataGrid(); } } 
+7
source

You need to catch the answer, for example:

 if (window.confirm) { //handle the OK } else { //handle the cancel } 
+1
source

Please show the code, but without seeing my code, I would assume that you are not returning an answer?

 <input type="button" onclick=" return confirm('is this right?'); "> 
0
source

You can simply return from code or exit(0) .

 function fillGrid() { if( confirm("....") ) { //populate your grid } else { return; } //do anything after the grid is populated //and exit the function } 
0
source

You show a confirmation dialog in the code of your button click, which does not stop the flow of your code.

You need to show a confirmation dialog before posting back

 <asp:Button ID="btnCopy" runat="server" Text="Copy" Enabled="true" OnClientClick="return confirm('sure?');" ValidationGroup="Copy" /> 
0
source

Source: https://habr.com/ru/post/904110/


All Articles