- Button - Javascript Confirm dialog - execute SOME server-side code?

I have a simple page where users can edit information about themselves.

When "Edit" button is clicked, the form goes into edit mode and I display "Save" and "Cancel" buttons, which behave as expected.

What I want to do is this:

When "Save" is clicked, display a Javascript Confirm dialog asking the user if they want to send an email to the other users to inform them of the update just made.

If user says OK, then execute all server-side code to save the data, AND send an email. If user says Cancel, then execute all server-side code to save the data, WITHOUT sending the email.

So, I need the javascript box to set a flag which can then be read server-side, somehow... then I can do something like:

Sub btnSave_Click(sender, e) Handles btnSave.Click

    'Save all the data

    If sendEmail Then  'This flag set by reading result of javascript Confirm
        'Send the email
    End If

End Sub

I know how to add a Confirm box to the button Attributes, and have done so. I'm looking for an answer on how to read the result of that box on server side... in other words, I ALWAYS want the Page postback to happen (from clicking the button), but only SOME of the event-handler code to execute.

Hope that makes sense.





Create a hidden field, and set the value of that field based on the result of the confirmation. You haven't shown the code/HTML for your button or form, but can you fit something like this into it:

<input type="hidden" id="sendEmail" name="sendEmail" value="" />

<input type="submit" value="Save" onclick="promptForEmail();" />

   function promptForEmail() {
      var result = Confirm("Send everybody an email?");
      // set a flag to be submitted - could be "Y"/"N" or "true"/"false"
      // or whatever suits
      document.getElementById("sendEmail").value = result ? "Y" : "N";

There are several ways to do it but I am going to use asp:HiddenField. In javascript, after user confirms, let its result to be set in the hidden field. And in server side, you can access it like any other control.


your aspx:

   <asp:HiddenField ID="HiddenField1" runat="server" Value="" />


  protected void Page_Load(object sender, EventArgs e)


      if (!Page.IsPostBack)

        var result = HiddenField1.Value; 


 //after confirm call this
 function SetValue(val)


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.