asp.net如何在前台利用jquery Ajax调用后台方法
一 :最近因为帮同事开发项目使用到了asp.net,而我又想实现Ajax异步请求....从网上查询了一下资料之后,原来在asp.net中利用Ajax调用后台方法同样很简单,为了便于自己以后查看,特将此整理后记录如下
先贴上前台代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="aspnetAjax.Index" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>
<%=Title %></title>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<style type="text/css">
td
{
text-align: center;
}
</style>
<script type="text/javascript">
$(function () {
$("#but").attr("disabled", false);
});
//(全/反)选:
function getChk() {
if ($("#ch").attr("checked")) {
$(".chk").attr("checked", true);
} else {
$(".chk").attr("checked",false);
}
}
//利用AJAX将数据发送到后台并判断数据选中情况方法:
function sendData() {
var sendValue = "";
$(".chk").each(function (i,chk) {
if (chk.checked) {
sendValue += $.trim($(chk).parent().parent().find(".getData").text());
sendValue += ",";
}
})
if (sendValue.length == 0) {
alert("请选择需要传输的数据!!");
return;
}
sendValue = sendValue.substr(0, sendValue.length - 1);
//开始ajax to asp.net:
$.ajax({
type: "post",
contentType: "application/json",
url: "Index.aspx/GetAjaxValue",
data: "{sendValue:'" + sendValue + "'}",
beforeSend: function () {
$("#but").attr("disabled", true);
},
success: function (data) {
alert(data.d);
}, complete: function () { }, error: function () { }
})
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width="400px"; border="1px solid blue" cellpadding="0px" cellspacing="0px" style="border-color: Green; text-align:center">
<tr>
<th>
全选<input type="checkbox" id="ch" onclick="getChk()" />
</th>
<th>
数据
</th>
</tr>
<tr>
<td>
<input type="checkbox" class="chk" />
</td>
<td class="getData">
1234
</td>
</tr>
<tr>
<td>
<input type="checkbox" class="chk" />
</td>
<td class="getData">
5678
</td>
</tr>
<tr>
<td>
<input type="checkbox" class="chk" />
</td>
<td class="getData">
12345678
</td>
</tr>
<tr>
<td>
<input type="checkbox" class="chk" />
</td>
<td class="getData">
87654321
</td>
</tr>
</table>
</div>
</form>
<hr />
<input type="button" id="but" onclick="sendData()" value="将选中的数据利用Ajax发送到后台" style=" background-color:#0080BB; cursor:pointer; border:1px solid #008094; border-radius:5px;" />
</body>
</html>
后台处理代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Script.Serialization;
using System.Web.Services; namespace aspnetAjax
{
public partial class Index : System.Web.UI.Page
{
public string title = "asp.net中使用Ajax";
protected void Page_Load(object sender, EventArgs e)
{ }
[WebMethod]
public static string GetAjaxValue(string sendValue)
{
return sendValue;
}
}
}
注意事项说明1:

注意事项说明二:

最终实现:

天气寒冷,就先写到这里....
asp.net如何在前台利用jquery Ajax调用后台方法的更多相关文章
- jquery + ajax调用后台方法
前台js: var parameter = ""; $.ajax({ type: "POST", //提交方式 url: "Default.aspx/ ...
- Jquery ajax调用后台aspx后台文件方法(不是ashx)
在asp.net webForm开发中,用Jquery ajax调用aspx页面的方法常用的有两种:下面我来简单介绍一下. (1)通过aspx.cs的静态方法+WebMethod进行处理 简单的介绍下 ...
- jQuery ajax调用后台aspx后台文件的两种常见方法(不是ashx)
在asp.net webForm开发中,用Jquery ajax调用aspx页面的方法常用的有两种:下面我来简单介绍一下. [WebMethod] public static string SayHe ...
- java 中使用ajax调用后台方法注意事项
java 中使用ajax调用后台方法注意事项,后台方法一定要加@ResponseBody jQuery.validator.addMethod("checkRuleName",fu ...
- Asp.net中JQuery、ajax调用后台方法总结
通过上一篇文章实例的实现,整个过程当中学习到很多知识点,了解了Jquery.Ajax在asp.net中的运用,加以总结,其实原理都是一样的,理解了一种,其他的注意很少的区别就可以了.灵活运用: 1.有 ...
- webform中 ajax调用后台方法(非webservice)
方法一:通过创建一个没有内容的窗体 后台: public partial class Ajax_ShoppingCart : System.Web.UI.Page { bookdbDataContex ...
- asp.net core 的 razor pages 如何使用ajax调用后台方法
Razor 是一种允许您向网页中嵌入基于服务器的代码(Visual Basic 和 C#)的标记语法. 当网页被写入浏览器时,基于服务器的代码能够创建动态内容. 在网页加载时,服务器在向浏览器返回页面 ...
- JS常用方法总结,及jquery异步调用后台方法实例
//前台接收get参数值 function getQueryString(name) { var queryStrings = window.location.search.sp ...
- C#实现简易ajax调用后台方法
在当前WEB当中,有些人都会抛弃asp.net的服务器控件,转而使用ajax来进行数据的交互和存储. 当我们大量使用ajax的时候,对于新手而言,肯定会创建很多的ashx或aspx页面,通过拼接参数, ...
随机推荐
- Eclipse: The superclass “javax.servlet.http.HttpServlet” was not found on the Java Build Path
Link: http://stackoverflow.com/questions/22756153/the-superclass-javax-servlet-http-httpservlet-was- ...
- float了的元素和内联元素不支持margin:auto
float了的元素和内联元素不支持margin:auto
- 51Nod 1046 A^B Mod C Label:快速幂
给出3个正整数A B C,求A^B Mod C. 例如,3 5 8,3^5 Mod 8 = 3. Input 3个正整数A B C,中间用空格分隔.(1 <= A,B,C <= 10^ ...
- git 设置多项目实现多账号登陆
9:45 2015/11/18git 设置多项目时实现多账号用户登陆git config --global user.name "your_name" git config --g ...
- InterBase数据库迁移到MySQL(恢复备份)
我拿到的是InterBase导出的“.gbk”后缀的数据库备份文件,目标是可以通过命令行的方式导入到指定的数据库中,在这个脚本中我使用了InterBase数据库中自带的“gbak”命令行来进行操作. ...
- push splice filter用法
checkedData.push(record); 直接在record 这个数组后面添加; var index =jQuery.inArray(record,checkedData);// 获取ind ...
- Bin Packing
Bin Packing 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/F 题目: A set of ...
- Linux下安装配置Apache+PHP+MariaDB
一.安装apache 1.下载并安装apache yum install httpd 2.启动apache systemctl start httpd.service 3.停止apache syste ...
- hdu Prime Ring Problem
简单的dfs,貌似这道题用暴力枚举就可以了,毕竟数据开的是比较小的. #include"iostream" #include"algorithm" #inclu ...
- PHP filesystem attack vectors
http://www.ush.it/2009/02/08/php-filesystem-attack-vectors/ On Apr 07, 2008 I spoke with Kuza55 and ...