jQuery的asp.net按钮通过ajax点击事件 [英] Jquery asp.net Button Click Event via ajax
问题描述
我想知道如果任何人都可以在正确的方向指向我。 我有一个asp.net按钮点击事件(即运行一些服务器端code)。 我想要做的是通过AJAX和jQuery调用此事件。 有没有办法做到这一点?如果是的话,我会喜欢一些例子。
I was wondering if anyone can point me in the right direction. I have an asp.net button with a click event (that runs some server side code). What i'd like to do is call this event via ajax and jquery. Is there any way to do this? If so, i would love some examples.
在此先感谢
推荐答案
这是jQuery的真正出色的ASP.Net开发。可以说,你有这样的ASP按钮:
This is where jQuery really shines for ASP.Net developers. Lets say you have this ASP button:
在呈现,你可以看网页的源代码和它的ID不会btnAwesome,但$ ctr001_btnAwesome或类似的东西。这使得它成为讨人厌找到的JavaScript。进入jQuery的。
When that renders, you can look at the source of the page and the id on it won't be btnAwesome, but $ctr001_btnAwesome or something like that. This makes it a pain in the butt to find in javascript. Enter jQuery.
$(document).ready(function() {
$("input[id$='btnAwesome']").click(function() {
// Do client side button click stuff here.
});
});
中的id $ =正在做一个正则表达式匹配的ID与btnAwesome结局。
The id$= is doing a regex match for an id ENDING with btnAwesome.
编辑:
你想要的Ajax调用从客户端上的按钮点击事件被称为?你想怎么称呼?有很多真正的好文章对使用jQuery做AJAX调用后面的方法ASP.Net code。
Did you want the ajax call being called from the button click event on the client side? What did you want to call? There are a lot of really good articles on using jQuery to make ajax calls to ASP.Net code behind methods.
它的要点是你创建一个静态标记有WebMethod属性的方法。然后,您可以打电话到它使用jQuery使用$阿贾克斯。
The gist of it is you create a static method marked with the WebMethod attribute. You then can make a call to it using jQuery by using $.ajax.
$.ajax({
type: "POST",
url: "PageName.aspx/MethodName",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
// Do something interesting here.
}
});
我知道我的WebMethod的东西从:<一href="http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/">http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/
很多真正的好ASP.Net/jQuery的东西在那里。请务必阅读了为什么你必须使用msg.d在NET 3.5的回报(也许因为3.0)的东西。
A lot of really good ASP.Net/jQuery stuff there. Make sure you read up about why you have to use msg.d in the return on .Net 3.5 (maybe since 3.0) stuff.
这篇关于jQuery的asp.net按钮通过ajax点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!