简体   繁体   中英

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

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

 <script type = "text/javascript" language = "javascript">
        function confirm_proceed()
        {
            if (confirm("Are you sure you want to proceed?")==true)
                return true;
            else
                return false;
        }
    </script> 

C#

  Button2.Attributes.Add("onclick", "return confirm_proceed();");

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.

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.

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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM