Today I will share with you the "simplest Asp.net Ajax Post", which is only applicable to beginners.
Opening lecture:
As we all know, Ajax is generally used for Asp.net to achieve the effect of refreshing data. There is also a lot of information on the Internet, but I want to record this simple example, so I came to the blog Park to write an essay...
Check the front-endCode:
HTML and JS
<% @ Page Language = " C # " Autoeventwireup = " True " Codebehind = " Index. aspx. CS " Inherits = " Jquery. admin. Index " %>
<! 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 > Demo </ Title >
< Link Href = "CSS/adminindex.css" REL = "Stylesheet" Type = "Text/CSS" />
< Script Type = "Text/JavaScript" >
// Create xhlhttprequest
Function Creatxmlhttprequest ()
{
// IE browser
If (Window. activexobject)
{
Return New Activexobject ( " Microsoft. XMLHTTP " );
}
Else
{
Return New XMLHttpRequest ();
}
}
// Click
Function Lookuser ()
{
VaR Xhr = Creatxmlhttprequest ();
VaR URL = " Getdata. ashx " ;
VaR ID = Document. getelementbyid ( " Txtuserid " ). Value;
// Concatenates a string to the backend for processing.ProgramAnd must be transcoded using escape. Otherwise, garbled characters may occur.
VaR Canshu = " Userid = " + Escape (ID );
// Method of execution after successful processing
Xhr. onreadystatechange = Function ()
{
If (Xhr. readystate = 4 && Xhr. Status = 200 )
{
Document. getelementbyid ( " Userinfo " ). Innerhtml = Xhr. responsetext;
}
}
Xhr. Open ( " Post " , URL, True );
// The header information can be called by the server through post.
Xhr. setRequestHeader ( " Content-Type " , " Application/X-WWW-form-urlencoded " );
// Send request
Xhr. Send (canshu );
}
</ Script >
</ Head >
< Body >
< Form ID = "Form1" Runat = "Server" >
< ASP: textbox ID = "Txtuserid" Runat = "Server" Width = "150px" Height = "22px" > </ ASP: textbox > & Nbsp;
< Button Type = "Button" Onclick = "Lookuser ()" Style = "Height: 30px; width: 80px ;" > View users </ Button > < BR /> < BR />
< Div ID = "Userinfo" >
</ Div >
</ Form >
</ Body >
</ Html >
One getdata. ashx is used to process the program and returns a string.
Effect
It's very simple. There's nothing to say, that's it...
PS: Tandy tang I wish you a pleasant time writing code in the New Year (2012!