The simplest Asp.net Ajax post is suitable for beginners.

Source: Internet
Author: User

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.