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>");
}
This question is related to
c#
javascript
asp.net
There are several options. You can use the WebMethod attribute, for your purpose.
The Jayrock RPC library is a great tool for doing this in a nice familliar way for C# developers. It allows you to create a .NET class with the methods you require, and add this class as a script (in a roundabout way) to your page. You can then create a js object of your type and call methods as you would any other object.
It essentially hides away ajax implementation and presents RPC in a familliar format. Mind you the best option really is to use ASP.NET MVC and use jQuery ajax calls to action methods - much more concise and less messing about!
I'm going to go right ahead and offer a solution using jQuery, which means you will need to import the library if you haven't already...
Import the jQuery library in your page mark-up:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
Then create another *.js script file (I call mine ExecutePageMethod
, since that is the only method it is going to expose) and import it:
<script type="text/javascript" src="/ExecutePageMethod.js" ></script>
Within the newly added file, add the following code (I remember pulling this from elsewhere, so someone else deserves credit for it really):
function ExecutePageMethod(page, fn, paramArray, successFn, errorFn) {
var paramList = '';
if (paramArray.length > 0) {
for (var i = 0; i < paramArray.length; i += 2) {
if (paramList.length > 0) paramList += ',';
paramList += '"' + paramArray[i] + '":"' + paramArray[i + 1] + '"';
}
}
paramList = '{' + paramList + '}';
$.ajax({
type: "POST",
url: page + "/" + fn,
contentType: "application/json; charset=utf-8",
data: paramList,
dataType: "json",
success: successFn,
error: errorFn
});
}
You will then need to augment your .NET page method with the appropriate attributes, as such:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string MyMethod()
{
return "Yay!";
}
Now, within your page mark-up, within a script
block or from another script file, you can call the method, like so:
ExecutePageMethod("PageName.aspx", "MyMethod", [], OnSuccess, OnFailure);
Obviously you will need to implement the OnSuccess
and OnFailure
methods.
To consume the results, say in the OnSuccess
method, you can use the parseJSON method, which, if the results become more complex (in the case or returning an array of types, for instance) this method will parse it into objects:
function OnSuccess(result) {
var parsedResult = jQuery.parseJSON(result.d);
}
This ExecutePageMethod
code is particularly useful since it it reusable, so rather than having to manage an $.ajax
call for each page method you might want to execute, you just need to pass the page, method name and arguments to this method.
You will need to do an Ajax call I suspect. Here is an example of an Ajax called made by jQuery to get you started. The Code logs in a user to my system but returns a bool as to whether it was successful or not. Note the ScriptMethod and WebMethod attributes on the code behind method.
in markup:
var $Username = $("#txtUsername").val();
var $Password = $("#txtPassword").val();
//Call the approve method on the code behind
$.ajax({
type: "POST",
url: "Pages/Mobile/Login.aspx/LoginUser",
data: "{'Username':'" + $Username + "', 'Password':'" + $Password + "' }", //Pass the parameter names and values
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
error: function (jqXHR, textStatus, errorThrown) {
alert("Error- Status: " + textStatus + " jqXHR Status: " + jqXHR.status + " jqXHR Response Text:" + jqXHR.responseText) },
success: function (msg) {
if (msg.d == true) {
window.location.href = "Pages/Mobile/Basic/Index.aspx";
}
else {
//show error
alert('login failed');
}
}
});
In Code Behind:
/// <summary>
/// Logs in the user
/// </summary>
/// <param name="Username">The username</param>
/// <param name="Password">The password</param>
/// <returns>true if login successful</returns>
[WebMethod, ScriptMethod]
public static bool LoginUser( string Username, string Password )
{
try
{
StaticStore.CurrentUser = new User( Username, Password );
//check the login details were correct
if ( StaticStore.CurrentUser.IsAuthentiacted )
{
//change the status to logged in
StaticStore.CurrentUser.LoginStatus = Objects.Enums.LoginStatus.LoggedIn;
//Store the user ID in the list of active users
( HttpContext.Current.Application[ SessionKeys.ActiveUsers ] as Dictionary<string, int> )[ HttpContext.Current.Session.SessionID ] = StaticStore.CurrentUser.UserID;
return true;
}
else
{
return false;
}
}
catch ( Exception ex )
{
return false;
}
}
Source: Stackoverflow.com