5

With ASP.NET, how do I prompt the user for a yes/no question and getting the result back to my .ascx?

So far I can open a confirmation dialog with use of Javascript, but I can't return the value. But I don't know if this is the right approach.

radbyx
  • 9,352
  • 21
  • 84
  • 127

8 Answers8

4

You can use standart JavaScript confirm() function to show popup and do Post Back in case of Yes or No. For example:

if (confirm('Question')) {
    __doPostBack('', 'Yes_clicked');
} else {
    __doPostBack('', 'No_clicked')
}  

Then on server in Page_Load() method do:

if (IsPostBack)
{
    var result = Request.Params["__EVENTARGUMENT"];
}

You can also do it async by specifying the first parameter of __doPostBack() function as ID of any update panel.

Sergey Metlov
  • 25,747
  • 28
  • 93
  • 153
  • Great! __doPostBack was just what I was looking for. And actually __EVENTARGUMENT was pretty handy to know also. First tests shows that it works great. – radbyx May 03 '11 at 12:27
  • :) Glad to help. Do not forget to accept if of all of your tests is passed. – Sergey Metlov May 03 '11 at 15:00
1

This is not a good practice to do this. you can get your confirm using javascript and postback or callback result to server.

but if you want to do this, this will help you :

A Simple ASP.NET Server Control: Message Box & Confirmation Box

Farzin Zaker
  • 3,578
  • 3
  • 25
  • 35
1

add this in head of source

function confirm_Edit() { if (confirm("Are you sure want to Edit?")==true) return true; else return false; }

call it like this

Vikky
  • 752
  • 3
  • 15
  • 35
1

If you insist on using webforms, another solution could be the AJAX Control kit. Simply create a ModalPopup and have you confirm buttons inside that.

Read more here: http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/ModalPopup/ModalPopup.aspx

jhovgaard
  • 560
  • 4
  • 18
0

I use this. As far as I know it prevents the rest of the button event from executing.

btnMyButton.Attributes.Add("onClick", "return confirm('Are you really sure?')");
Janspeed
  • 2,644
  • 2
  • 22
  • 22
0

Another option is to show yes/no:

<script>     
        function AlertFunction() {
            if (confirm('Are you sure you want to save this thing into the database?')) {
               $('#ConfirmMessageResponse').val('Yes');
            } else {
                $('#ConfirmMessageResponse').val('No');
            }
        }
    </script>

to handle it from .net side:

string confirmValue = ConfirmMessageResponse.Value;
                if (confirmValue == "Yes")
                {...}
elifekiz
  • 1,456
  • 13
  • 26
0

You need to use ajax, or to make a postback to the server. Your c# code is server side and the javascript is client side. If you use the ajax extensions for asp .net you can use javascript page methods:

PageMethods.YourMethod(confirm('your text'), OnSuccess, OnFailure);
shrutyzet
  • 529
  • 3
  • 11
-1

string confirmValue = ConfirmMessageResponse.Value; showing error in this line when using in .net side

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 10 '22 at 08:00