How to call the confirmation window in the IF state from the code behind?

I use LinkButton and DropDown.

When I click the LinkButton button, DropDown appears.

After selecting DropDown, I want a confirmation box from JavaScript to appear to change the value.

I call this script in the second if state, but it does not work.

After confirmation, I want to change another value and exit the condition.

 protected void lnkbtnSave_Click(object sender, EventArgs e) { if ((ddlHiringManager.SelectedItem != null && (ddlHiringManager.SelectedItem.Text != lblHiringManager.Text)) && (Convert.ToInt32(ddlHiringManager.SelectedValue)) != -1) { if (ClientScript.RegisterStartupScript(typeof(Page), "Confirm", "<script type='text/javascript'>Confirm('Are you sure you want to change Hiring Manager for this requirement.');</script>")) { ClsClientManager objClientManager = new ClsClientManager(); if (objClientManager.UpdateHRManagerByReqID(Convert.ToInt32(hdnReqId.Value), Convert.ToInt32(ddlHiringManager.SelectedValue)) > 0) { lblShowHiringManager.Text = ddlHiringManager.SelectedItem.Text; } } } else { ClientScript.RegisterStartupScript(typeof(Page), "SymbolError", "<script type='text/javascript'>alert('Please Select Hiring Manager !');</script>"); } } 
+4
source share
3 answers

You cannot use the result of the RegisterStartupScript method.

Change the ASPX page code for LinkButton as below

 <asp:LinkButton ID="lnkbtnSave" runat="server" OnClick="lnkbtnSave_Click" OnClientClick="javascript: return confirm('Are you sure you want to change Hiring Manager for this requirement.');">Save</asp:LinkButton> 

I have added a client side click event.

When you click the LinkButton button, you will receive a confirmation window. The page will only be canceled if you click OK in the confirmation window.

+1
source

See this code snippet. Changed Index Event Dropdown

  protected void ddlTest_SelectedIndexChanged(object sender, EventArgs e) { string str = "Are you sure, you want to upload leave ?"; this.ClientScript.RegisterStartupScript(typeof(Page), "Popup", "ConfirmApproval('" + str + "');", true); } 

And for the client side, declare this method.

 <script type="text/javascript"> function ConfirmApproval(objMsg) { if (confirm(objMsg)) { $('#divUploadLeave').fadeTo('slow', .6); return true; } else { $('#divUploadLeave').fadeTo('slow', 1); return false; } } 

Hope this helps you.

However, if you want everything on the client side, please let me know.

-1
source

Please add a refund before Confirm , this will solve your problem.

  **if (ClientScript.RegisterStartupScript(typeof(Page), "Confirm", "<script type='text/javascript'>return Confirm('Are you sure you want to change Hiring Manager for this requirement.');</script>"))** 
-1
source

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


All Articles