jquery 调用ashx方法,实现页面无刷新
2011-04-13 11:44
567 查看
本测试包含2个页面 Ajax.aspx 和 Handler1.ashx
先看看 Ajax.aspx
<head runat="server">
<title></title>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$().ready(function () {
$('#user-ajax').click(function () {
$.get("Handler1.ashx", { method: "method1", txt1: $("#Text1").text() }, function (msg) { $("#Text1").text(msg); } );
//或者 $.post("Handler1.ashx", { method: "method1", txt1: $("#Text1").text() }, function (msg) { $("#Text1").text//(msg); } );
});
});
</script>
</head>
<body>
<input id="user-ajax" type="button" value="click" />
<div id="Text1" style="height:100px;border:1px solid Gray; color:Blue;">initial data</div>
</body>
//说明
// 上面的method对应的键值"method1"是handler1.ashx里面的方法名(函数名),我在handler1.ashx里面写了3个方法可以选择性的使用
//这样我们就可以使用 id 为 user-ajax的按钮 实现页面局部更新了
再看看Handler1.ashx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Jquery
{
/// <summary>
/// Summary description for Handler1
/// </summary>
public class Handler1 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
String txt = context.Request["txt1"].ToString();
String method = context.Request["method"].ToString();
switch (method)
{
case "method1":
txt= Method1(txt);
break;
case "method2":
txt = Method1(txt);
break;
case "method3":
txt = Method1(txt);
break;
}
context.Response.Write(txt + "Hello World");
}
public string Method1(string para)
{
return para+" is dealed by method 1";
}
public string Method2(string para)
{
return para + " is dealed by method 3";
}
public string Method3(string para)
{
return para + " is dealed by method 3";
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
先看看 Ajax.aspx
<head runat="server">
<title></title>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$().ready(function () {
$('#user-ajax').click(function () {
$.get("Handler1.ashx", { method: "method1", txt1: $("#Text1").text() }, function (msg) { $("#Text1").text(msg); } );
//或者 $.post("Handler1.ashx", { method: "method1", txt1: $("#Text1").text() }, function (msg) { $("#Text1").text//(msg); } );
});
});
</script>
</head>
<body>
<input id="user-ajax" type="button" value="click" />
<div id="Text1" style="height:100px;border:1px solid Gray; color:Blue;">initial data</div>
</body>
//说明
// 上面的method对应的键值"method1"是handler1.ashx里面的方法名(函数名),我在handler1.ashx里面写了3个方法可以选择性的使用
//这样我们就可以使用 id 为 user-ajax的按钮 实现页面局部更新了
再看看Handler1.ashx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Jquery
{
/// <summary>
/// Summary description for Handler1
/// </summary>
public class Handler1 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
String txt = context.Request["txt1"].ToString();
String method = context.Request["method"].ToString();
switch (method)
{
case "method1":
txt= Method1(txt);
break;
case "method2":
txt = Method1(txt);
break;
case "method3":
txt = Method1(txt);
break;
}
context.Response.Write(txt + "Hello World");
}
public string Method1(string para)
{
return para+" is dealed by method 1";
}
public string Method2(string para)
{
return para + " is dealed by method 3";
}
public string Method3(string para)
{
return para + " is dealed by method 3";
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
相关文章推荐
- ASP.NET jQuery 食谱23 (jQuery AJAX实现调用页面后台方法和web服务定义的方法)
- 通过JQuery调用MVC的Controller方法来实现无刷新操作
- jQuery AJAX实现调用页面后台方法。调用ashx方法
- jquery实现刷新页面的几种方法介绍
- 使用jquery的load()方法实现页面局部刷新
- JQuery怎么实现页面刷新后保留鼠标点击样式的方法
- jquery实现form表单提交后局部刷新页面的多种方法
- mui 重写back 调用back方法,实现返回就即时刷新页面
- jQuery&nbsp;AJAX实现调用页面后台方法
- Jquery刷新页面背景图片随机变换的实现方法
- 通过JQuery调用MVC的Controller方法来实现无刷新操作
- 使用JQuery 加载页面时调用JS的实现方法
- Jquery实现form表单提交后局部刷新页面的多种方法
- Jquery刷新页面背景图片随机变换的实现方法
- jquery实现form表单提交后局部刷新页面的多种方法
- 通过JQuery调用MVC的Controller方法来实现无刷新操作
- js 调用ajax方法实现新增数据,原页面刷新
- jQuery实现AJAX定时局部页面刷新
- jquery实现页面局部刷新
- jQuery AJAX实现调用页面后台方法