一 :最近因为帮同事开发项目使用到了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调用后台方法的更多相关文章

  1. jquery + ajax调用后台方法

    前台js: var parameter = ""; $.ajax({ type: "POST", //提交方式 url: "Default.aspx/ ...

  2. Jquery ajax调用后台aspx后台文件方法(不是ashx)

    在asp.net webForm开发中,用Jquery ajax调用aspx页面的方法常用的有两种:下面我来简单介绍一下. (1)通过aspx.cs的静态方法+WebMethod进行处理 简单的介绍下 ...

  3. jQuery ajax调用后台aspx后台文件的两种常见方法(不是ashx)

    在asp.net webForm开发中,用Jquery ajax调用aspx页面的方法常用的有两种:下面我来简单介绍一下. [WebMethod] public static string SayHe ...

  4. java 中使用ajax调用后台方法注意事项

    java 中使用ajax调用后台方法注意事项,后台方法一定要加@ResponseBody jQuery.validator.addMethod("checkRuleName",fu ...

  5. Asp.net中JQuery、ajax调用后台方法总结

    通过上一篇文章实例的实现,整个过程当中学习到很多知识点,了解了Jquery.Ajax在asp.net中的运用,加以总结,其实原理都是一样的,理解了一种,其他的注意很少的区别就可以了.灵活运用: 1.有 ...

  6. webform中 ajax调用后台方法(非webservice)

    方法一:通过创建一个没有内容的窗体 后台: public partial class Ajax_ShoppingCart : System.Web.UI.Page { bookdbDataContex ...

  7. asp.net core 的 razor pages 如何使用ajax调用后台方法

    Razor 是一种允许您向网页中嵌入基于服务器的代码(Visual Basic 和 C#)的标记语法. 当网页被写入浏览器时,基于服务器的代码能够创建动态内容. 在网页加载时,服务器在向浏览器返回页面 ...

  8. JS常用方法总结,及jquery异步调用后台方法实例

    //前台接收get参数值 function getQueryString(name) {            var queryStrings = window.location.search.sp ...

  9. C#实现简易ajax调用后台方法

    在当前WEB当中,有些人都会抛弃asp.net的服务器控件,转而使用ajax来进行数据的交互和存储. 当我们大量使用ajax的时候,对于新手而言,肯定会创建很多的ashx或aspx页面,通过拼接参数, ...

随机推荐

  1. Storm和JStorm(阿里的流处理框架)

    本文导读: 1.What——JStorm是什么? 1.1 概述 .2优点 .3应用场景 .4JStorm架构 2.Why——为什么启动JStorm项目?(与storm的区别) .1storm的现状.缺 ...

  2. 推荐两款PC健康小软件

    一.前言 对于经常需要坐在电脑前工作一整天的人来说,健康问题是不得不关注的.下面推荐我一直在用的两款体积非常小(几百KB)的健康小软件,也许可以在无形中保护你.提醒你. 1. FadeTop 这是一款 ...

  3. 不刷新改变URL: pushState + Ajax

    如果你玩过Google+,看到过YouTube的新界面,便会体验到这个HTML5的新功能.使用pushState + Ajax(pjax),可以实现网页的ajax加载,同时又能完成URL的改变而没有网 ...

  4. Codeforces Round #242 (Div. 2) B. Megacity

    按照半径排序,然后累加人数直到超过百万 #include <iostream> #include <algorithm> #include <cmath> #inc ...

  5. 【BZOJ】3771: Triple

    http://www.lydsy.com/JudgeOnline/problem.php?id=3771 题意:n个带价值互不相同的物品,每次可以取1.2.3个物品,问能得到的所有的价值和这个价值的方 ...

  6. BZOJ2400: Spoj 839 Optimal Marks

    Description 定义无向图中的一条边的值为:这条边连接的两个点的值的异或值. 定义一个无向图的值为:这个无向图所有边的值的和. 给你一个有n个结点m条边的无向图.其中的一些点的值是给定的,而其 ...

  7. spring源码学习之路---深度分析IOC容器初始化过程(四)

    作者:zuoxiaolong8810(左潇龙),转载请注明出处,特别说明:本博文来自博主原博客,为保证新博客中博文的完整性,特复制到此留存,如需转载请注明新博客地址即可. 最近由于工作和生活,学习耽搁 ...

  8. __attribute__ 变量对齐

    http://blog.163.com/sunm_lin/blog/static/9192142200741533038695/ 一.   __attribute__ ((aligned (n))) ...

  9. Windows Phone 8 Sync

    A lot of the below depends on the types of data, how often it is changing, and how often it is likel ...

  10. Soldier and Bananas

    Soldier and Bananas 题目链接:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=173141 题意: 给 ...