ASP.net - Button - Javascript Confirm dialog - execute SOME server-side code?

后端 未结 2 1280
执念已碎
执念已碎 2021-01-19 07:27

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

When \"Edit\" button is clicked, the form goes into edit mode and I display \"Save\"

相关标签:
2条回答
  • 2021-01-19 07:54

    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 asp.net control.

    So

    your aspx:

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

    CodeBehind:

      protected void Page_Load(object sender, EventArgs e)
    
      {
    
          if (!Page.IsPostBack)
          {
    
            var result = HiddenField1.Value; 
          } 
      }
    

    Javascript:

     //after confirm call this
     function SetValue(val)
     {
       document.getElementById('HiddenField1').value=val;
     }
    
    0 讨论(0)
  • 2021-01-19 08:09

    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();" />
    
    <script>
       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";
       }
    </script>
    
    0 讨论(0)
提交回复
热议问题