Use Ajax in ASP. NET to translate from www.asp.net website) (1)

Source: Internet
Author: User

inscription :
not long ago, I saw this discussion in a foreigner's blog . , about why I wrote a blog, he said it was for the sake of forgetting , to understand your thoughts , Get a file-type memory . This makes sense . but this article , is a technical Article translated from someone else . maybe , is also for the sake of forgetting !

description :

ASP. NET pages are based on the return Mode,This makes sense.,If you want to execute server-side events,The client must return the page.This event-driven model,Certainly more powerful,However, it must rewrite the page every time it is returned..Most browsers currently supportJSAndDHTML. AjaxThis mode enables users to experience the operation more intelligently and conveniently..In the first part,I will use a simple example to explain this model..

AjaxWhat is?

It is asynchronous.JSAndXMLAbbreviation.It is not a new technology. Ie, HTML, XML, Dom, XMLHTTPHas existed and used for many years, AjaxIs to merge the parts separated from each other.,To work.

Imagine,If there is a button,Tag,And text box webpage,Text Box to hold oneID,When you clickButtonWill be displayed in the tagIDUser Name,What do you do?

You will write the related information in your button eventIDUser Information Process,Send another callback event,However, if you have many widgets on your webpage,That must be rewritten every time the request is sent back.,Is it expensive?,Of course, it will affect the processing speed..

Ajax solves this problem.With this, you no longer need to go back to the webpage frequently.,Instead, you can send a request.,Server-side Processing,Then return,The corresponding control will be overwritten.,Display the required value.

On the one hand,This mode can give users a better experience.,And can increase the speed,But on the other hand, you need to consider whether your browser supportsJS.

A simple example:

To experience this mode of work,Let's give an example.,This example includes two web pages,One of them has a button and tag.,This web page willAjaxTo call the second web page..The second web page has no user interface,Content not directly displayed.WhenLoadIt is used to output"Hello world ",The string is displayed on the label of the first form..

1>UseVs. netCreateWebApplicationProgram

2>Add a form,Put one button and one tag
3>Before writingLoadEvent write process

Private sub page_load (byval sender as system. object,
Byval e as system. eventargs) handles mybase. Load
Button2.attributes. Add ("onclick ",
"Return getdataviaajax ();")
If page. ispostback then
Label1.text = "server date time:" & datetime. Now
End if
End sub

Here we want to add a client to handle the event for this button,Let's show the tag the date of the server.,But it does not appear on tags.,Because there is no callback event at all,This is just to verify,Then,InHtmlPage file<Head>Add the followingJSMethod:

VaR OBJ;

   

 
Function getdataviaajax ()

 
{

 
Try

 
{

 
OBJ = new activexobject ("msxml2.xmlhttp ");

 
}

 
Catch (E)

 
{

Try

 
{

 
OBJ = new activexobject ("Microsoft. XMLHTTP ");

 
}

 
Catch (E1)

 
{

 
OBJ = NULL;

 
}

 
}

 
If (OBJ! = NULL)

{

 
OBJ. onreadystatechange = processresponse;

 
OBJ. Open ("get ",

 
"Http: // localhost/ajaxdemo/helloworld. aspx ",True );

 
OBJ. Send (null );

 
}

 
Return false;

 }

  Here we instantiate XMLHTTP Class , This is Ajax Key to the model , It can obtain server resources asynchronously. . Try... catch Block to prevent , MSXML Exceptions caused by different versions .
This class Onreadystatechange This attribute is used to call Processresponse Method , Processresponse This method is used to process the obtained data. ,
Then , We pass Get Or Post Call Open Method , Last executed Send Method , Suppose you use Post You can pass Parameters , Here, we use Get So we don't need to consider the parameters. .

 

 

 
Function processresponse ()

 
{
 
If (obj. readystate = 4)

 
{

 
If (obj. Status = 200)

 
{

 
VaR retval = obj. responsetext;

Alert (retval );

 
}

 
Else

 
{

 
Alert ("error retrieving data! ");

 
}

 
}

 }< br>   hellowork  ,  This  processresponse  calls .  test the  rreadystate  attribute value ,  if it is  4 , the request is ,  If the  Code  status value is  200,  indicates  OK. 
finally, we use responsetext to obtain the returned data. , another responsexml attribute is used to obtain the returned XML data ( If )
Add the following code to the load event on the second webpage: :

 Private sub page_load (byval sender as system. object,

 
Byval e as system. eventargs) handles mybase. Load

 
Response. Clear ()

 
Response. Write ("Hello World ")

 
 Response. End ()

End sub
Here , First clear Respone, Send a string . Set the first page to start page , Start the project , View results .

ExampleSource codeDownload

Summary:

In this article , We see how to useAjax To help us handle the response and improve the efficiency . Similarly , We also saw XMLHTTP component How to test the water IE Of .
Next section , I will describe more details , More complex acquisition XML Method .

Remarks:Individual level,Errors are inevitable in the article.,Please forgive me!This article is translated from Www.asp.net, Cannot be used for commercial purposes,If reprinted, indicate the source.

 

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.