Ajax. NET User Development Guide (5)

Source: Internet
Author: User
Tags add call back end error handling httpcontext integer net string
Ajax| Development Guide
[Ajax.ajaxmethod]

public string Test1 (string name, string email, string comment) {
String html = "";
HTML + + "Hello" + name + "
";
html = "Thank for your comment ";
HTML + System.Web.HttpUtility.HtmlEncode (comment);
HTML + = "
.";
return HTML;
}

SessionState

In the service-side function, you may need to accept the delivered session information, in order to do this, you must pass a parameter on the Ajax.ajaxmethod attribute of the service-side function that you want to implement this way .

When looking at Ajax to support the session, let's look at other features first. . In the following example, we have a document management system that locks the document when a user edits it, and other users need to wait until the document is available to modify . Without Ajax, users need to keep waiting to refresh because they have to constantly check that the status of the document is available, which is certainly not a good solution. . It's easier to support it with the AJAX session state .

We first write a function that, by traversing the document ID, finds the document that the user needs, stores it in the session, and returns the document that is not occupied:

' VB.net

httpsessionstaterequirement.read) > _
Public Function documentreleased () as ArrayList
If HttpContext.Current.Session ("documentswaiting") is nothing Then
Return Nothing
End If
Dim readydocuments as New ArrayList
Dim documents () As Integer = CType (HttpContext.Current.Session ("documentswaiting"), Integer ())
For I as an Integer = 0 to documents. Length-1
Dim Document as document = document. Getdocumentbyid (Documents (i))
If not the document is nothing AndAlso document. Status = Documentstatus.ready Then
Readydocuments.add (document)
End If
Next
Return readydocuments
End Function

C#
[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;
}
}

We indicated the httpsessionstaterequirement.read in the attribute argument (also can be write and ReadWrite)

The following writes the JavaScript function to use this method to bring the result:



Every 10 seconds after the page is loaded, the server function is requested once . If there is a return, the call back function checks the response and displays the latest results .

 Conclusion

Ajax technology can provide a rich customer experience for clients, and Ajax.NET provides the possibility for you to achieve such powerful functionality easily.

    • Ajax: A new way to build Web apps
    • Discussion on the error handling mechanism of AJAX (2)
    • Discussion on the error handling mechanism of AJAX (1)
    • First experience. NET Ajax Brushless New technology
    • A brief analysis of Ajax development Technology in Rails system (4)


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.