Search code examples
javascriptc#asp.netconfirm

ASP.NET C# backend, did the user press OK or Cancel from JavaScript confirm box?


In the C# backend code for a website, I have the following code that displays a message for the user, enumerating the data they are about to submit, and asking for human-confirmation that the information appears accurate.

This is executed within C#, not within JavaScript, so how can I access the result of the confirm function?
My C# logic needs to follow a different path if they click OK, versus if they click Cancel.

I'm working on a very old application (13 years), that has been untouched for quite some time.
As a result, I don't have the ability to alter the general design of the application.

ClientScript.RegisterStartupScript(this.GetType(), "InvalidEntrySubTypeAlert", "confirm('" +
    "Are you sure you want to create the following deal?\n" +
    "\nDeal ID :\t\t\t" + nDealID +
    "\nCompany Name :\t\t" + txtCompanyName.Text +
    "\nEntry Subject :\t\t" + txtEntrySubject.Text +
    "\nBusiness Lead :\t\t" + ddlBusinessLead.SelectedItem.Text +
    "\nLicense Status :\t\t" + ddlLicenseStatus.SelectedItem.Text +
    "\nEffective Date :\t\t" + string.Format("{0:MM/dd/yyyy}", calEffectiveDate.SelectedDate) +
    "\nExpiration Date :\t\t" + string.Format("{0:MM/dd/yyyy}", calExpirationDate.SelectedDate) +
   "\nLicense Location :\t\t" + txtLicenseLocation.Text +
    "\nEntry Sub Type :\t\t" + ddlEntrySubType.SelectedItem.Text.Split(' ')[0]
    + "');", true);

Solution

  • You can use a HiddenField to set the value from the client side and then access it on the Server.

    Here is a mockup

    <asp:HiddenField ID="hndSetFromJS" runat="server"></asp:HiddenField>
    

    And set the value like this

    document.getElementById("#<%= hndSetFromJS.ClientID %>").value = "yourValue";
    

    or

    $("#<%= hndSetFromJS.ClientID %>").val("yourValue"); // Jquery