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();");

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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>

Method 2

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.

Method 3

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x