The case explains how to use jquery post in asp.net, asp. netjquery
I. post case:
1. Foreground default. aspx:
<Html xmlns = "http://www.w3.org/1999/xhtml">
<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>
</Head> <body> <div class = "divFrame"> <div class = "divTitle"> <span> Name: </span> <input id = "txtName" type = "text" class = "txt"/> <select id = "selSex" style = "height: 22px; margin-right: 3px "> <option value =" "> select gender </option> <option value =" male "> male </option> <option value =" female "> female </ option> </select> <input id = "Button1" type = "button" class = "btn" value = "request"/> </div> <div class = "divContent ""> <div id =" divTip "> </div> </body> 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");
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 = "<div class = 'clsshow '>"; // initialize and save the Content Variable if (strName = "<span style =" font-family: Arial, Helvetica, sans-serif; "> Tao guorong </span>" & strSex = "<span style =" font-family: Arial, Helvetica, sans-serif; "> male </span>") {strHTML + = "Name: Tao guorong <br>"; strHTML + = "Gender: male <br> "; strHTML + = "Mailbox: tao_guo_rong@163.com
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" %> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
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>
<asp:TextBox ID="txtName" runat="server" class="txt" ></asp:TextBox>
<Select id = "selSex" style = "height: 22px; margin-right: 3px "> <option value =" "> select gender </option> <option value =" male "> male </option> <option value =" female "> female </ option> </select> <input id = "Button1" type = "button" class = "btn" value = "request"/> </div> <div class = "divContent ""> <div id =" divTip "> </div> </form> </body>
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 = "<div class = 'clsshow '> "; // initialize and save the Content Variable if (strName = "Tao guorong" & strSex = "male") {strHTML + = "Name: Tao guorong <br> "; strHTML + = "Gender: male <br>"; strHTML + = "Email: tao_guo_rong@163.com
Result:
The csdn editor seems to be a bit problematic. The fixed numbers are all labeled and changed several times, so we have to separate the highlighted parts into segments separately. I hope you will forgive me.