Jquery asp.net按钮点击事件通过ajax [英] Jquery asp.net Button Click Event via ajax

查看:118
本文介绍了Jquery asp.net按钮点击事件通过ajax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道有没有人可以指出我正确的方向。
我有一个带有click事件的asp.net按钮(运行一些服务器端代码)。
我想做的是通过ajax和jquery调用这个事件。
有什么办法吗?如果是这样,我会喜欢一些例子。



提前感谢

解决方案

p>这是jQuery真正为ASP.Net开发人员发光的地方。让我们说你有这个ASP按钮:





当渲染时,你可以看看页面和其上的id不会是btnAwesome,但$ ctr001_btnAwesome或类似的东西。这使得在javascript中找到屁股的痛苦。输入jQuery。

 
$(document).ready(function(){
$(input [id $ ='btnAwesome '])点击(function(){
//客户端按钮点击此处
});
});

id $ =正在使用与btnAwesome的id ENDING进行正则表达式匹配。



您是否希望从客户端的按钮点击事件调用ajax调用?



你想叫什么?有很多非常好的文章,使用jQuery来做ajax调用ASP.Net代码的方法。



它的要点是你创建一个静态方法标有WebMethod属性。然后,您可以使用jQuery通过$ .ajax来调用它。

 
$ .ajax({
type: POST,
url:PageName.aspx / MethodName,
data:{},
contentType:application / json; charset = utf-8,
dataType:json,
success:function(msg){
//在这里做一些有趣的事情
}
});

我从: http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax page-methods /



很多非常好的ASP.Net/jQuery东西在那里。确保你阅读关于为什么你必须在.Net 3.5(可能是3.0)的东西中使用msg.d。


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.

Thanks in advance

解决方案

This is where jQuery really shines for ASP.Net developers. Lets say you have this ASP button:

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.
  });
});

The id$= is doing a regex match for an id ENDING with btnAwesome.

Edit:

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.

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.
  }
});

I learned my WebMethod stuff from: http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆