Using AJAX power in JavaScript Confirmation window

I am pretty sure everyone knows about the JavaScript confirmation window if not you can look at the image below:

Confirmation Window

Anyway, sometimes we need to capture "ok" as well as "Cancel". "Ok" is easy to catch since the server side button click will take place if you click "ok". Catching "cancel" is kinda hard since cancel is only handled on the client side. What if you want to do something when the cancel button is clicked. You can use AJAX to solve this problem.

Here is a simple example. Below are the two server side methods that will be called from the client side:

Server Side Methods:

[Ajax.AjaxMethod]

public string UpdateMethod()

{

// Do your update thing here

return "Yes I want to dance. I am also a sissy guy";

}

[Ajax.AjaxMethod]

public string AddMethod()

{

// Do your Adding in the database here

return "No way! I am a hotty.";

}

JavaScript Code:

<script language="javascript"> 
        
        
// Check confirmation method 
        
        
function CheckConfirmation() 
        {
            var result = confirm(
'Are you sure you want to dance with a sissy girl'); 
            
if(result) 
            {
            
// Call the Update method  
            
AjaxDemo.UpdateMethod(UpdateMethod_CallBack);     
            
            } 
            
else 
            
{
                
// Call the Add method  
                
AjaxDemo.AddMethod(AddMethod_CallBack); 
            }
        }
        
        function AddMethod_CallBack(response) 
        {
            var myString = response.
value
            alert(myString);  
        }
                    
        function UpdateMethod_CallBack(response) 
        {
            var myString = response.
value;
            alert(myString);                        
        }

</script>

If this all code is too much for you than it mean that you need to read my article AJAX IN ACTION.

 

 

 

 

 

 

powered by IMHO

 

Print | posted @ Wednesday, September 28, 2005 2:10 AM

Twitter