Complete simple Ajax chat room

Source: Internet
Author: User
Tags ajax chat

Completed the first Ajax LearningProgram. This chat room program is in. NET environment, with the most basic chat room functions, registration, login, speak, display online user list... in. net. In short, it is very easy to modify the web. config File, add Ajax. DLL reference and write to serverCodeAdd the [Ajax. ajaxmethod ()] attribute declaration, indicates that the server code is the method to be called by JS Code, the class corresponding to the C # method called by JS Code is registered in the page_load event, js code writing, and the JS Code triggering event is added.. After completing these items, You can normally apply Ajax technology.

1. web. config File Modification: in the web. in the config file. add the httphandlers node to the Web node, and add a subnode under the node to add verb = "post, get" Path = "ajax /*. ashx "type =" Ajax. pagehandlerfactory, Ajax ":

2. in Visual Studio. to add a reference to. net, right-click the reference node in solution Resource Manager, select "add reference", click the Browse button in the pop-up tab, and select the correct Ajax. dll path. Click OK.

3. write code like a general C # class and method, just add the [Ajax. ajaxmethod ()] attribute declaration to the C # method, indicating that this is an Ajax method.

4. register the class corresponding to the Ajax method in page_load (that is, the class of the Ajax method). Add the following code to the page_load event: Ajax. utility. registertypeforajax (typeof (yourclass); yourclass is your class name. the JS Code can call the Ajax method of the class through a specific method only when the class is registered first.

5. There are some methods to write js code. I will take a piece of code first:

Function getonlineuser_callback (response) ---------------------------------------------- B

{

VaR STR = new string ();

VaR uliststr = new string ();

VaR udiv = Document. getelementbyid ("ulist ");

If (response. Error! = NULL)

{

Alert (response. Error );

}

STR = response. value; uliststr = s tr. Replace ("| ","");

Udiv. innerhtml = uliststr;

}

Function getonline () ---------------------------------------------------------------

{

Main. getonlineuser (getonlineuser_callback );

}

here, there are two functions A and B, where function a getonline () is the JS Code that is triggered (for example, The onclick event in the button is medium ...), here, getonline () calls a method with the getonlineuser_callback parameter, which is calling the C # code (server side ), the specific call method I mentioned above is a class with the same name. call the server-side code (Ajax method) in the form of a method with the same name ). Main. getonlineuser () has a "fixed parameter", which is the entry of another JS function (that is, the function name). Here it is getonlineuser_callback, which indicates that it is a fixed parameter and the meaning is: even if there is no parameter in the Ajax method, there will be such a parameter, that is to say, when calling the Ajax method in JS, there will be one more parameter than the corresponding C # method, that is, the function name with the _ callback suffix (this function name is custom, not always end with _ callback. This is just a habit ). the callback function has a parameter, namely, response, which has a value attribute, which stores the values returned from the Ajax method (C # method). It can be a simple type or dataset, datatable and other types. You can use response. Value to update elements on the page. In addition, response has another property error, which stores the error information. You can determine whether the error is correct or not based on whether the error information is null. The Ajax technology can be applied through this simple step... Development Environment: winxpsp2, Visual Studio. net2003, C #

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.