Case Study on jquery post usage in asp.net
I. post case:
1. Foreground default. aspx:
<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>
<Script type = text/javascript> $ (function () {$ (# Button1 ). click (function () {// click the event // open the file and return the server response data through the callback function $. post (Handler. ashx, {name: encodeURI ($ (# txtName ). val (), sex: encodeURI ($ (# selSex ). val ()}, function (data) {$ (# divTip ). empty () // clears the content in the tag first. html (data); // display the data returned by the server}) </script>
Name: Select genderMaleFemale 2. handler. ashx in the background
<%@ 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);
<% @ Webhandler = "" language = "C #" class = "Handler">
String strName = System. web. httpUtility. urlDecode (context. request [name]); // decodes the name string strSex = System. web. httpUtility. urlDecode (context. request [sex]); // decodes gender characters
string strHTML =
; // Initialize and save the Content Variable if (strName = Tao guorong & strSex = male) {strHTML + = Name: Tao guorong
; StrHTML + = Gender: male
; StrHTML + = mailbox: tao_guo_rong@163.com ;} Else if (strName = Li Jianzhou & strSex = female) {strHTML + = Name: Li Jianzhou
; StrHTML + = Gender: Female
; StrHTML + = mailbox: xiaoli@163.com ;} StrHTML + =; context. response. write (strHTML); // context. response. write (return); // If a character is returned directly, the data in the foreground function is a pure string "return"} public bool IsReusable {get {return false ;}}}
3. Description:
1. In the passed parameters
Name and the corresponding parameter value encodeURI ($ (# txtName ). val (), which can be represented as {name: encodeURI ($ (# txtName) in the function ). val ()),}
Format, you can also add single quotes to both sides. The parameters can be obtained in the ashx file.
2. encodeURI ($ (# txtName ). val () is a form of processing Chinese characters, and the corresponding background needs to use System. web. httpUtility. urlDecode (Request [name]) for Chinese character decoding
3. Try to use the ashx file response in the background to accelerate the corresponding page speed.
4. If context. Response. Write (return) is used directly, the data in the foreground function will get the pure string "return" (excluding html)
4. Results
Additional cases of using server controls:
1. Front-end
<%@ page="" language="C#" autoeventwireup="true" codefile="Default.aspx.cs" inherits="_Default">
<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> <Script type = text/javascript> $ (function () {$ (# Button1 ). click (function () {// click the event // open the file and return the server response data through the callback function $. post (Handler. ashx ,{
name: $(input[id*=txtName]).val(),
Sex: $ (# selSex ). val ()}, function (data) {$ (# divTip ). empty () // clears the content in the tag first. html (data); // display the data returned by the server })})}) </script> <% @ page = "" language = "C #" autoeventwireup = "true" codefile = "Default. aspx. cs "inherits =" _ Default ">
Select genderMaleFemale
The background is the same as the previous case:
<% @ 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]); // decodes the name string strSex = System. web. httpUtility. urlDecode (context. request [sex]); // decodes gender Characters string strHTML =
; // Initialize and save the Content Variable if (strName = Tao guorong & strSex = male) {strHTML + = Name: Tao guorong
; StrHTML + = Gender: male
; StrHTML + = mailbox: tao_guo_rong@163.com
;} Else if (strName = Li Jianzhou & strSex = female) {strHTML + = Name: Li Jianzhou
; StrHTML + = Gender: Female
; StrHTML + = mailbox: xiaoli@163.com
;} StrHTML + =; context. Response. Write (strHTML); // context. Response. Write (return);} public bool IsReusable {get {return false ;}}}
Result: