在asp.net web form 中使用ajax
首先可以先去看看一篇兄弟文章:http://blog.csdn.net/mfcdestoryer/article/details/22041975
里面有一些背景知识。
首先看效果图:
这是整个项目工程的文件图:
同上面的兄弟文章一样,用的也是ashx文件来接受ajax的post请求。
ashx文件可以在新建项时找到。
首先,建立一个ashx文件,名为Handler2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace webFormAjax
{
/// <summary>
/// Handler2 的摘要说明
/// </summary>
public class Handler2 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string flg = "用户名可用";
if (context.Request.Form["name"] == "123")
{
//这里就可以是你自己的数据库操作判断
flg = "不可用";
}
context.Response.Write(flg);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
代码说明,
参数HttpContext context包含了post请求的所有内容,
所有可以利用context提取很多信息!!
{小知识扩展:
asp.net 中Request.Form["XXX"]可以读取post传进的参数
Request.QueryString["XXX"]可以读取get传入的参数}
上面的代码就是用if语句判断后,利用Response.Write给前台返回信息的!
第二步,新建一个 web from 叫做WebForm2
后台代码没有任何修改,
请看前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="webFormAjax.WebForm2" %>
<!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>
<script src="Content/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$("#check").click(function () {
$.ajax({
type: "POST",
url: "Handler2.ashx",
data: "name=" + $("#TextBox1").val(),
success: function (responseData) {
$("#s1").empty();
$("#s1").text(responseData);
}
});
})
})
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<input type="button" id="check" value="check" />
<span id="s1"></span>
</p>
</div>
</form>
</body>
</html>
代码说明:
首先引入了jquery的js文件
然后写了一个脚本,
主要是针对input的click事件写ajax,
可以看到ajax里面的事件是post事件,指向的url就是刚刚写的Handler2,
传过去的参数叫name,值是TextBox1的内容,
因为Handler2里面可以根据HttpContext获得Request.Form["name"]的值,
然后Response.Write(flg)在ajax的success函数中显示。
总结:在ashx文件中可以用HttpContext获得ajax的请求内容
首先可以先去看看一篇兄弟文章:http://blog.csdn.net/mfcdestoryer/article/details/22041975
里面有一些背景知识。
首先看效果图:
这是整个项目工程的文件图:
同上面的兄弟文章一样,用的也是ashx文件来接受ajax的post请求。
ashx文件可以在新建项时找到。
首先,建立一个ashx文件,名为Handler2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace webFormAjax
{
/// <summary>
/// Handler2 的摘要说明
/// </summary>
public class Handler2 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string flg = "用户名可用";
if (context.Request.Form["name"] == "123")
{
//这里就可以是你自己的数据库操作判断
flg = "不可用";
}
context.Response.Write(flg);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
代码说明,
参数HttpContext context包含了post请求的所有内容,
所有可以利用context提取很多信息!!
{小知识扩展:
asp.net 中Request.Form["XXX"]可以读取post传进的参数
Request.QueryString["XXX"]可以读取get传入的参数}
上面的代码就是用if语句判断后,利用Response.Write给前台返回信息的!
第二步,新建一个 web from 叫做WebForm2
后台代码没有任何修改,
请看前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="webFormAjax.WebForm2" %>
<!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>
<script src="Content/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$("#check").click(function () {
$.ajax({
type: "POST",
url: "Handler2.ashx",
data: "name=" + $("#TextBox1").val(),
success: function (responseData) {
$("#s1").empty();
$("#s1").text(responseData);
}
});
})
})
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<input type="button" id="check" value="check" />
<span id="s1"></span>
</p>
</div>
</form>
</body>
</html>
代码说明:
首先引入了jquery的js文件
然后写了一个脚本,
主要是针对input的click事件写ajax,
可以看到ajax里面的事件是post事件,指向的url就是刚刚写的Handler2,
传过去的参数叫name,值是TextBox1的内容,
因为Handler2里面可以根据HttpContext获得Request.Form["name"]的值,
然后Response.Write(flg)在ajax的success函数中显示。
总结:在ashx文件中可以用HttpContext获得ajax的请求内容