在asp.net web form 中使用ajax
在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的請求內容
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.