Get the return confirm popbox value in asp .net C#

后端 未结 3 970
遇见更好的自我
遇见更好的自我 2021-01-23 09:10

How can i get the value that was pressed in the confirm box?

 

        
相关标签:
3条回答
  • 2021-01-23 09:50

    I just face similar problem in a real production project and I solved it by the following:

    <asp:Button ID="btn1" runat="server" OnClick="Button1_Click" onClientClick="return confirm('Are you sure you want to proceed?')"/>
    

    so the OnClientClick Client event is raised befoere the onClick which is a server event , so if the user clicks OK then the Client event returns True from the confirm Dialog and therefore the Code Behind this button is executed , on the other hand if the user clicks (Cancel or No) then it would return false and therefore the code behind wont get exected (Server Event is Cancelled)

    hope it would help you as I really applied it to my project and worked without any issues.

    0 讨论(0)
  • Try this, if this is the only button that has this behavior

    Button2.Attributes.Add("onclick", "return confirm('Are you sure you want to proceed?')");
    

    it's inline and looks straightforward but if you have multiple controls that behave this way then your original approach would be easy to maintain.

    And your original function could be shrunken to

     <script type = "text/javascript" language = "javascript">
            function confirm_proceed()
            {
                return confirm("Are you sure you want to proceed?");
            }
     </script> 
    
    0 讨论(0)
  • 2021-01-23 10:03

    You can store the value of confirm_proceed() in an asp:HiddenField

    You can modify your script as follows:

     <script type = "text/javascript" language = "javascript">
            function confirm_proceed()
            {
                var hiddenField = document.getElementById('hiddenFieldId');
    
                if (confirm("Are you sure you want to proceed?")==true)
                {
                    hiddenField.value = 'true';
                    return true;
                }
                else
                {
                    hiddenField.value = 'false';
                    return false;
                }
            }
     </script> 
    

    You can now access first the hidden field's value in your Button2_Click event.

    0 讨论(0)
提交回复
热议问题