Does anyone know how to call a server-side c# method using javascript? What i need to do is to stop imports if Cancel is chosen or to continue importing if ok is chosen. I am using visual studio 2010 and c# as my programming lanaguage
This is my code:
private void AlertWithConfirmation()
{
Response.Write(
"<script type=\"text/javascript\">" +
"if (window.confirm('Import is currently in progress. Do you want to continue with importation? If yes choose OK, If no choose CANCEL')) {" +
"window.alert('Imports have been cancelled!');" +
"} else {" +
"window.alert('Imports are still in progress');" +
"}" +
"</script>");
}
PageMethod an easier and faster approach for Asp.Net AJAX
We can easily improve user experience and performance of web applications by unleashing the power of AJAX. One of the best things which I like in AJAX is PageMethod.
PageMethod is a way through which we can expose server side page’s method in java script. This brings so many opportunities we can perform lots of operations without using slow and annoying post backs.
In this post I am showing the basic use of ScriptManager and PageMethod. In this example I am creating a User Registration form, in which user can register against his email address and password. Here is the markup of the page which I am going to develop:
To setup page method, first you have to drag a script manager on your page.
Also notice that I have changed
EnablePageMethods="true".This will tell ScriptManager that I am going to call PageMethods from client side.
Now next step is to create a Server Side function.
Here is the function which I created, this function validates user’s input:
To tell script manager that this method is accessible through javascript we need to ensure two things:
First: This method should be ‘public static’.
Second: There should be a [WebMethod] tag above method as written in above code.
Now I have created server side function which creates account. Now we have to call it from client side. Here is how we can call that function from client side:
To call my server side method Register user, ScriptManager generates a proxy function which is available in PageMethods.
My server side function has two paramaters i.e. email and password, after that parameters we have to give two more function names which will be run if method is successfully executed (first parameter i.e. onSucess) or method is failed (second parameter i.e. result).
Now every thing seems ready, and now I have added
OnClientClick="Signup();return false;"on my Signup button. So here complete code of my aspx page :