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.
- // C #: ASP. net ajax example
- [Ajax. AjaxMethod ()]
- Public StaticArrayList Search (StringSearch)
- {
- SearchQuery query =NewSearchQuery ();
- Query. PageIndex = 0;// Obtain the first 10 results
- Query. PageSize = 10;
- Query. UserID = Users. GetUser (). UserID;
- Query. SearchTerms = search;
- Return NewForumSearch (). GetSearchResults (query). Posts;
- }
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.
- // C #: ASP. net ajax example
- 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.
- // C #: ASP. net ajax example
- [Serializable]
- Public ClassForumPost: Post
- {
- ...
- }
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.
- < asp:textbox onBlur="Search(this.value);"
- id="PostSubject" runat="server" ... />
Next, we compile the JavaScript Search method to call the server-side Search.
- Var oldValue ='';
- Function Search (value)
- {
- // Do not search for the content you just searched again
- // If you move the Tab key backward or forward
- If(Value! = OldValue)
- {
- CreateEditPost. Search (value, Search_CallBack );
- OldValue = value;
- }
- }
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.
- Function Search_CallBack (response)
- {
- // The search function is automatically redirected when no result is found,
- // Therefore, we cannot use response. error.
- Var results = response. value;
- // If no result is obtained
- If(Results =Null)
- {
- Return;
- }
- // DIV used to place the result
- Var someDiv = document. getElementById ("SomeDiv");
- Var html ="";
- For(Var I = 0; I <results. length; ++ I)
- {
- Var result = results [I];
- Html + ="<A target = _ blank href = '"+ Result. PostID
- Html + ="/ShowPost. aspx '>";
- Html + = result. Subject;
- Html + ="</A> <br/>"
- }
- SomeDiv. innerHTML = html;
- }
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.
- ASP. net ajax example: Document lock Program
- ASP. net ajax example: drop-down list
- AJAX. NET installation and Configuration Guide
- Ajax. Net Quick Start
- Download ASP. net ajax Software