案例講解asp.net中jquery post的用法,asp.netjquery
一、post案例:
1.前台default.aspx:
<html xmlns="http://www.w3.org/1999/xhtml"><head> <title>$.post發送請求</title> <script type="text/javascript" src="Jscript/jquery-1.4.2-vsdoc.js"> </script> <script type="text/javascript" src="Jscript/jquery-1.4.2.js"> </script> <style type="text/css"> body{font-size:13px} .divFrame{width:260px;border:solid 1px #666} .divFrame .divTitle{padding:5px;background-color:#eee;height:23px} .divFrame .divTitle span{float:left;padding:2px} .divFrame .divContent{padding:8px} .divFrame .divContent .clsShow{font-size:14px} select,input{float:left} .txt{border:#666 1px solid;padding:2px;width:80px;margin-right:3px} .btn {border:#666 1px solid;padding:2px;width:50px; filter: progid:DXImageTransform.Microsoft.Gradient(GradientType=0,StartColorStr=#ffffff, EndColorStr=#ECE9D8);} </style>
<script type="text/javascript"> $(function() { $("#Button1").click(function() { //按鈕單擊事件 //開啟檔案,並通過回呼函數返回伺服器響應後的資料 $.post("Handler.ashx", { name: encodeURI($("#txtName").val()), sex: encodeURI($("#selSex").val()) }, function(data) { $("#divTip") .empty() //先清空標記中的內容 .html(data); //顯示伺服器返回的資料 }) }) }) </script>
</head><body> <div class="divFrame"> <div class="divTitle"> <span>姓名:</span> <input id="txtName" type="text" class="txt" /> <select id="selSex" style="height:22px;margin-right:3px"> <option value="">選性別</option> <option value="男">男</option> <option value="女">女</option> </select> <input id="Button1" type="button" class="btn" value="請求" /> </div> <div class="divContent"> <div id="divTip"></div> </div> </div></body></html>
2.後台handler.ashx
<%@ WebHandler Language="C#" Class="Handler" %>using System;using System.Web;public class Handler : IHttpHandler { public void ProcessRequest (HttpContext context) { //context.Response.ContentType = "text/plain"; //context.Response.Write("Hello World");
string strName = System.Web.HttpUtility.UrlDecode(context.Request["name"]);//解碼姓名字元 string strSex = System.Web.HttpUtility.UrlDecode(context.Request["sex"]);//解碼性別字元
string strHTML = "<div class='clsShow'>"; //初始化儲存內容變數 if (strName == "<span style="font-family: Arial, Helvetica, sans-serif;">陶國榮</span>" && strSex == "<span style="font-family: Arial, Helvetica, sans-serif;">男</span>") { strHTML += "姓名:陶國榮<br>"; strHTML += "性別:男<br>"; strHTML += "郵箱:tao_guo_rong@163.com<hr>"; } else if (strName == "<span style="font-family: Arial, Helvetica, sans-serif;">李建洲</span>" && strSex == "<span style="font-family: Arial, Helvetica, sans-serif;">女</span>") { strHTML += "姓名:李建洲<br>"; strHTML += "性別:女<br>"; strHTML += "郵箱:xiaoli@163.com<hr>"; } strHTML += "</div>"; context.Response.Write(strHTML); //context.Response.Write("return");//如直接返回字元,前台function中的data則為純字串“return” } public bool IsReusable { get { return false; } }}
3.說明:
1.傳遞的參數中的
name和對應的參數值encodeURI($("#txtName").val()),在函數中即可以表示為{name:encodeURI($("#txtName").val()),}
形式,也可以加對雙方加單引號,ashx檔案都可以得到參數值
2.encodeURI($("#txtName").val())是對遇到漢字的處理形式,相應的後台需要使用System.Web.HttpUtility.UrlDecode(Request["name"])來進行漢字解碼
3.後台盡量使用ashx檔案響應,會使得的頁面相應速度加快
4.如直接使用context.Response.Write("return"),前台function中的data將得到純字串“return”(不含html)
4.結果
另外附加使用伺服器控制項的案例:
1.前台
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!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> <title>$.post發送請求</title> <script type="text/javascript" src="Jscript/jquery-1.4.2-vsdoc.js"> </script> <script type="text/javascript" src="Jscript/jquery-1.4.2.js"> </script> <style type="text/css"> body{font-size:13px} .divFrame{width:260px;border:solid 1px #666} .divFrame .divTitle{padding:5px;background-color:#eee;height:23px} .divFrame .divTitle span{float:left;padding:2px} .divFrame .divContent{padding:8px} .divFrame .divContent .clsShow{font-size:14px} select,input{float:left} .txt{border:#666 1px solid;padding:2px;width:80px;margin-right:3px} .btn {border:#666 1px solid;padding:2px;width:50px; filter: progid:DXImageTransform.Microsoft.Gradient(GradientType=0,StartColorStr=#ffffff, EndColorStr=#ECE9D8);} </style> <script type="text/javascript"> $(function () { $("#Button1").click(function () { //按鈕單擊事件 //開啟檔案,並通過回呼函數返回伺服器響應後的資料 $.post("Handler.ashx", {
name: $("input[id*=txtName]").val(),
sex: $("#selSex").val() }, function (data) { $("#divTip") .empty() //先清空標記中的內容 .html(data); //顯示伺服器返回的資料 }) }) }) </script></head><body><form runat=server> <div class="divFrame"> <div class="divTitle"> <span>姓名:</span>
<asp:TextBox ID="txtName" runat="server" class="txt" ></asp:TextBox>
<select id="selSex" style="height:22px;margin-right:3px"> <option value="">選性別</option> <option value="男">男</option> <option value="女">女</option> </select> <input id="Button1" type="button" class="btn" value="請求" /> </div> <div class="divContent"> <div id="divTip"></div> </div> </div></form></body></html>
後台與前面案例相同:
<%@ WebHandler Language="C#" Class="Handler" %>using System;using System.Web;public class Handler : IHttpHandler { public void ProcessRequest (HttpContext context) { //context.Response.ContentType = "text/plain"; //context.Response.Write("Hello World"); string strName = System.Web.HttpUtility.UrlDecode(context.Request["name"]);//解碼姓名字元 string strSex = System.Web.HttpUtility.UrlDecode(context.Request["sex"]);//解碼性別字元 string strHTML = "<div class='clsShow'>"; //初始化儲存內容變數 if (strName == "陶國榮" && strSex == "男") { strHTML += "姓名:陶國榮<br>"; strHTML += "性別:男<br>"; strHTML += "郵箱:tao_guo_rong@163.com<hr>"; } else if (strName == "李建洲" && strSex == "女") { strHTML += "姓名:李建洲<br>"; strHTML += "性別:女<br>"; strHTML += "郵箱:xiaoli@163.com<hr>"; } strHTML += "</div>"; context.Response.Write(strHTML); //context.Response.Write("return"); } public bool IsReusable { get { return false; } }}
結果顯示:
csdn的編輯器好像有點問題哦,著重號都變成了標籤來表示,改了好幾次,只好將著重部分單獨分隔成一段了,希望大家見諒