How can I using jquery to get a data from a function in aspx.cs in .net c# web application?
Does anyone know how can I using jquery to retrieve the data from the function in aspx.cs in .net c# web application?
Example:
if I have a function call public void getData(string serachValue)
. When the user type i开发者_StackOverflow中文版n the word in the textbox, it will use jquery to call getData() to return the result and show on the screen
First make this function static and add webmethod attribute:
[WebMethod]
public static string Confirm(int id)
{
// confirm entry
return "Test Data";
}
Then use ajax() jquery method: http://api.jquery.com/jQuery.ajax/
$('.confirmBtn').click(function () {
$.ajax({
type: "POST",
url: "MyPageName.aspx/Confirm",
data: '{"id":"' + 1 + '"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
var returnData = data.d;
},
error: function (xhr, ajaxOptions, thrownError) {
alert(thrownError.toString());
},
timeout: function (data) {
}
});
});
And the best article talking about this is there: http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/
In Asp.Net MVC you'd create a controller action which calls getData()
and returns Json data, or a partial view. In JQuery you'd then call that action using it's URL in .load()
, or something similar.
If the method is in the code behind you can tag decorate it with [WebMethod]
[WebMethod]
public static void getData(string serachValue) { ... }
Check out this blog for the jquery call and JS you will need. http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/
精彩评论