Example of ASP. net ajax: Forum topic search

Source: Internet
Author: User

Example of ASP. net ajax: Forum topic search

The last example is to modify the existing application. The first time I heard this idea was that Josh Ledgard imagined adding a feature to the MSDN forum. The goal is to help users find their own answers and limit the number of repeated releases. Generally, when a user raises a new question in a forum, he or she will enter the subject and question. Generally, they do not search to see if they have already asked and answered the question. Enter AJAX. After a topic is entered and the Tab key is removed from the topic, the Forum is searched asynchronously Based on the topic and the results are displayed to the user in a timely manner. Sometimes these results are helpful, and sometimes they are not.

To make the results helpful, We will modify asp. NETPRO Reader's Choice Award for Best Forum Application and CommunityServer. The downloadable example does not contain this part or forum) code, but you can learn more about CommunityServer at http://communityserver.org/english. You can also apply the following code snippets.

After installing CommunityServer and configuring Ajax. NET to add the reference and processing programs to web. config), we only need to make some changes to obtain the required functions. First, go to the CreateEditPost. cs file in the CommunityServerForums project. It is treated as the code after this page, where you can add a new release. Next we will add a function that enables AJAX.

 
 
  1. // C #: ASP. net ajax example 
  2. [Ajax. AjaxMethod ()]
  3. Public StaticArrayList Search (StringSearch)
  4. {
  5. SearchQuery query =NewSearchQuery ();
  6. Query. PageIndex = 0;// Obtain the first 10 results 
  7. Query. PageSize = 10;
  8. Query. UserID = Users. GetUser (). UserID;
  9. Query. SearchTerms = search;
  10. Return NewForumSearch (). GetSearchResults (query). Posts;
  11. }

We can use the search function that has been created in CommunityServer. We only need our function to apply it. As usual, the type must be registered using Ajax. NET. We will perform this operation in the InitializeSkin function of the same file as Page_Load.

 
 
  1. // C #: ASP. net ajax example 
  2. Ajax. Utility. RegisterTypeForAjax (Typeof(CreateEditPost ));

Before redirecting to JavaScript, we need to make the final server-side changes. The custom class returned to Ajax. NET, such as the ForumPost contained in the ArrayList we are returning, must be labeled with the Serializable attribute. To add this attribute, go to the Components/ForumPost. cs file in the CommunityServerForums project.

 
 
  1. // C #: ASP. net ajax example 
  2. [Serializable]
  3. Public ClassForumPost: Post
  4. {
  5. ...
  6. }

When displaying, we only need to modify Themes/default/Skins/View-EditCreatePost.cs in the CommunityServerWeb project. First, we will trigger the onBlur event of the topic text box.

 
 
  1. < asp:textbox onBlur="Search(this.value);" 
  2. id="PostSubject" runat="server" ... /> 

Next, we compile the JavaScript Search method to call the server-side Search.

 
 
  1. Var oldValue ='';
  2. Function Search (value)
  3. {
  4. // Do not search for the content you just searched again 
  5. // If you move the Tab key backward or forward 
  6. If(Value! = OldValue)
  7. {
  8. CreateEditPost. Search (value, Search_CallBack );
  9. OldValue = value;
  10. }
  11. }

Finally, the rest is to process the response. As the previous example shows a slightly more formal way to display results in a table, we will create some dynamic HTML and paste it into the virtual DIV.

 
 
  1. Function Search_CallBack (response)
  2. {
  3. // The search function is automatically redirected when no result is found, 
  4. // Therefore, we cannot use response. error. 
  5. Var results = response. value;
  6. // If no result is obtained 
  7. If(Results =Null)
  8. {
  9. Return;
  10. }
  11. // DIV used to place the result 
  12. Var someDiv = document. getElementById ("SomeDiv");
  13. Var html ="";
  14. For(Var I = 0; I <results. length; ++ I)
  15. {
  16. Var result = results [I];
  17. Html + ="<A target = _ blank href = '"+ Result. PostID
  18. Html + ="/ShowPost. aspx '>";
  19. Html + = result. Subject;
  20. Html + ="</A> <br/>" 
  21. }
  22. SomeDiv. innerHTML = html;
  23. }

By adding web. config for configuration to the three files of the CommunityServer application, we can add some useful functions. However, you must be careful when adding the AJAX feature to an existing application. The pre-existing ForumSearch class for actual search may not be designed for the usage type we introduced. Our code may lead to some additional searches, and the impact may be significant.

The above is an example of ASP. net ajax: implementation of Forum topic search.

  1. ASP. net ajax example: Document lock Program
  2. ASP. net ajax example: drop-down list
  3. AJAX. NET installation and Configuration Guide
  4. Ajax. Net Quick Start
  5. Download ASP. net ajax Software

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.