ASP. net ajax example: Document lock Program

Source: Internet
Author: User

ASP. net ajax example: Document lock Program

This example is a simple document management system. Like any formal document management system, we must provide concurrency management. That is, we need a method to solve the problem of two users trying to edit the same document. We will create some type of locking mechanism to prevent the edited documents from being edited by another user, so as to achieve the above purpose. We will use AJAX to give users a more pleasant locking experience. First, we will create a document queue for users to try to edit but cannot edit because other users are editing this document), and will automatically notify users when these documents are available. Second, we will ensure that the document is unlocked when the user closes the browser or navigating to another location. The next feature helps ensure that the document is never locked. To this end, we will skip functions unrelated to AJAX implementation in this Guide; however, downloadable projects include all functions.

First, when you try to edit a document, we will try to create an exclusive lock for it. If it fails, we will add this document to the user's queue and then return it to the home page. There is nothing special about AJAX here, but we will look at the Code to give the necessary context for the example. In the OnLoad event of the Page for editing, add the following code.

 
 
  1. // C #: ASP. net ajax example 
  2. If(! Page. IsPostBack)
  3. {
  4. // User input should be verified 
  5. Document document = GetDocument (Request. QueryString ["Id"]);
  6. // We have this document but cannot edit it! 
  7. If(! Locker. AcquireLock (document ))
  8. {
  9. // Let's add it to the list of user documents to be viewed 
  10. User. CurrentUser. AddDocumentToQueue (document. Your entid );
  11. Response. Redirect ("DocumentList. aspx");
  12. }
  13. // Well, we have this document and can edit it. 
  14.  //... 
  15. }

The location of the key line is to add the document to the queue of the current user, which will add the document to the session ). Next, we will create a user control that can be placed on any page to notify users when queue documents are available. This user control will contain an AJAX method and the Code required to register the AJAX class.

 
 
  1. 'Vb. NET: ASP. net ajax example 
  2. Private SubPage_Load (sAs Object, EAsEventArgs)
  3. Handles MyBase. Load
  4. Ajax. Utility. RegisterTypeForAjax (GetType(UnlockNotifier ))
  5. End Sub 
  6. 'Traverse queue documents and check whether they are available 
  7. <Ajax. AjaxMethod ()> _
  8. Public FunctionGetUnlockedDocuments ()AsDocumentCollection
  9. 'Get the ID of all queue documents of the user 
  10. DimQueuedDocumentAsArrayList = User. CurrentUser. DocumentQueue
  11. DimUnlockedAsDocumentCollection =NewDocumentCollection
  12. For EachEntidAs Integer InQueuedDocumentIds
  13. 'If the queue document is no longer locked 
  14. If NotLocker. IsLocked (entid)Then 
  15. Unlocked. Add (Document. GetDocumentById (entid ))
  16. End If 
  17. Next 
  18. ReturnUnlockedDocuments

What End Function needs now is to make some JavaScript send requests and process the response. We will place published document information in the table to be dynamically created based on the response, if any ). To this end, we will begin to write HTML.

 
 
  1. < div id="notifyBox" style="display:none;"> 
  2. < b>The following queued documents can now be edited< /b> 
  3. < table cellpadding="5" cellspacing="0" 
  4. border="0" style="border:1px solid #EEE;" 
  5. id="notifyTable"> 
  6. < /table> 
  7. < /div> 

If no document is available or the document is not listed for this user), we use the DIV tag to hide all the content and use the TABLE tag to display the result. We will use the polling system to check whether any available queue documentation exists. In general, this means that we will always call the server method and display the result later. Only the first call occurs when the page is loaded, and subsequent calls occur every X seconds.

 
 
  1. <Script language ="Javascript">
  2. Window. setTimeout ("PollQueue ();", 2000 );
  3. // Calls every 2 seconds to check whether a release is made in the actual system with many users 
  4. // Queue document, which may take the server for 2 seconds 
  5. // Excessive load. We can even check whether the user is 
  6. // Has a queue, but we do need to do some 
  7. // Performance test 
  8. FunctionPollQueue ()
  9. {
  10. // UnlockNotifier is the type we registered using Ajax. NET. 
  11. // GetUnlockedDocuments is a method of this type, marked 
  12. // AjaxMethod Property 
  13. UnlockNotifier. GetUnlockedDocuments (PollQueue_CallBack );
  14. // Call itself every 2 seconds 
  15. Window. setTimeout ("PollQueue ();", 2000 );
  16. }
  17. </Script>

The rest is to process the response. This is similar to the Code in the previous example. First, check for errors, get responses, traverse available documents, and dynamically create HTML. In this case, Add rows and columns to the table.

 
 
  1. Function PollQueue_CallBack (response)
  2. {
  3. Var policybox = document. getElementById ("Yybox");
  4. Var policytable = document. getElementById ("Policytable");
  5. // If we cannot find the table notification box 
  6. If(Policybox =Null| Policytable =Null)
  7. {
  8. Return;
  9. }
  10. // If the server code is abnormal 
  11. If(Response. error! =Null)
  12. {
  13. Policybox. style. display ="None";
  14. Alert (response. error );// We should be able to do better 
  15. Return;
  16. }
  17. Var documents = response. value;
  18. // If it is not the response we want 
  19. If(Documents =Null|Typeof(Documents )! ="Object")
  20. {
  21. Policybox. style. display ="None";
  22. Return;
  23. }
  24. For(Var I = 0; I <policytable. rows. length; ++ I)
  25. {
  26. Policytable. deleteRow (I );
  27. }
  28. For(Var I = 0; I <documents. length; ++ I)
  29. {
  30. Var row = policytable. insertRow (0 );
  31. Row. className ="Row"+ I % 2;
  32. Var cell = row. insertCell (0 );
  33. Cell. innerHTML = documents [I]. Title;
  34. Cell = row. insertCell (1 );
  35. Var date = documents [I]. Created;
  36. Cell. innerHTML = date. getDay () +"/"+ Date. getMonth ()
  37. +"/"+ Date. getYear ();
  38. Cell = row. insertCell (2 );
  39. Cell. innerHTML ="<A href = 'deletedit. aspx? Id ="
  40. + Documents [I]. entid +"'> Edit </a>";
  41. }
  42. Policybox. style. display ="Block";
  43. }

The last quick improvement we want to see is that when you close your browser, navigate to another link, or click the "back" button, the document will be automatically unlocked. Generally, you can trigger the JavaScript OnBeforeUnLoad event or OnUnload Event to achieve this goal. This will open a new small pop-up window, which will be cleaned up when loading the page and closed by yourself. You can use a pop-up window by yourself, but others cannot use it. This will cause the pop-up window to be blocked and keep the document locked permanently. To solve this problem, we still need two JavaScript events, but instead of starting the pop-up window, we will execute the server-side method through AJAX. On the page for editing the document, that is, the page for placing the lock), we add some simple JavaScript.

 
 
  1. < Script Language="Javascript"> 
  2. // If you close your browser or click "back,
  3. // Make sure this document is unlocked
  4. Window. onbeforeunload=ReleaseLock;
  5. Function ReleaseLock (){
  6. Locker. ReleaseDocument (<% = Entid %>);
  7. }
  8. </Script> 

Here, entid is the variable defined and set in the subsequent code. In addition, we can store the entid in the session and access it in the ReleaseDocument on the server side. Usually, ReleaseDocument is deleted from the locked document list.

The above is an example of ASP. net ajax: implementation of the document lock program.

  1. ASP. net ajax example: drop-down list
  2. AJAX. NET installation and Configuration Guide
  3. Ajax. Net Quick Start
  4. Download ASP. net ajax Software
  5. The father of ASP. NET is strongly recommended: ASP. NET AJAX

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.