How to make a simple yes / no popup in ASP.NET that returns the result back to my C #?

With ASP.NET, how do I ask the user a yes / no question and return the result to my .ascx?

So far, I can open a confirmation dialog using Javascript, but I cannot return the value. But I do not know if this is correct.

+5
source share
7 answers

You can use the standard JavaScript confirm() function to display a popup and do Post Back in the case of Yes or No. For instance:

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

Then on the server in Page_Load() do:

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

You can also do this asynchronously by specifying the first parameter of the __doPostBack() function as the identifier of any update panel.

+4
source

This is not a good practice. you can get confirmation using javascript result and postback or callback to server.

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

Simple ASP.NET server control: message box and confirmation window

+1
source

add this to the top of the source

function confirm_Edit () {if (confirm ("Do you really want to edit?") == true) return true; else return false; }

name it like that

+1
source

If you insist on using web forms, the AJAX Control suite might be another solution. Just create ModalPopup and make sure it has buttons.

More details here: http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/ModalPopup/ModalPopup.aspx

+1
source

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

 PageMethods.YourMethod(confirm('your text'), OnSuccess, OnFailure); 
0
source

I am using this. As far as I know, this prevents the rest of the button event from executing.

 btnMyButton.Attributes.Add("onClick", "return confirm('Are you really sure?')"); 
0
source

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 the .net side:

 string confirmValue = ConfirmMessageResponse.Value; if (confirmValue == "Yes") {...} 
0
source

All Articles