Implement Ajax in ASP. NET (5)

Source: Internet
Author: User
Sessionstate

 

The server functions may need to access session information. Therefore, you only need to tell ajax to enable this function by passing a parameter to the Ajax. ajaxmethod attribute.

While examining the package's session capabilities, let's take a look at several other features. In this example, we have a document management system that locks documents when users edit them. Other users can request a notification when the document is available. Without Ajax, we can only wait for the user to return again to check whether the requested document is available. Obviously not ideal. It is very easy to use Ajax that supports session status.

First, write the server-side function. The objective is to traverse the entid that you want to edit (stored in the session) and return all released documents.

[Ajax. ajaxmethod (httpsessionstaterequirement. Read)]

Public arraylist documentreleased (){

If (httpcontext. Current. session ["documentswaiting"] = NULL ){

Return NULL;

}

Arraylist readydocuments = new arraylist ();

Int [] documents = (INT []) httpcontext. Current. session ["documentswaiting"];

For (INT I = 0; I <documents. length; ++ I ){

Document document = Document. getdocumentbyid (documents [I]);

If (document! = NULL & document. Status = documentstatus. Ready ){

Readydocuments. Add (document );

}

}

Return readydocuments;

}

}

Note that the value httpsessionstaterequirement. Read is specified (write and readwrite can also be used ).

Now write the Javascript that uses this method:

<Script language = "JavaScript">

Function documentsready_callback (response ){

If (response. Error! = NULL ){

Alert (response. Error );

Return;

}

If (response. value! = NULL & response. value. length> 0 ){

VaR DIV = Document. getelementbyid ("status ");

Div. innerhtml = "The following statements are ready! <Br/> ";

For (VAR I = 0; I <response. value. length; ++ I ){

Div. innerhtml + = "<a href = \" Edit. aspx? Entid = "+ response. value [I]. entid +" \ ">" + response. value [I]. Name + "</a> <br/> ";

}

}

SetTimeout ('page. documentreleased (documentsready_callback) ', 10000 );

}

</SCRIPT>

<Body onload = "setTimeout ('document. documentreleased (documentsready_callback) ', 10000);">

Our server-side functions are called every 10 seconds during page loading. The callback function checks the response to see if there is any returned value. If yes, the new document that this user can use is displayed in the DIV tag.

Conclusion

Ajax technology has given birth to a robust and rich web interface that was originally available only for desktop development. The Ajax. Net package allows you to easily use this new powerful technology. Please note that the Ajax. Net Package and documentation are still under development. Http://www.shpan.com/Detail.asp? Id = 400
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.