Design and Implementation of ASP. NET Website chat rooms (section 3rd), asp.net section 3rd

Source: Internet
Author: User

Design and Implementation of ASP. NET Website chat rooms (section 3rd), asp.net section 3rd

Everyone has ever played website chat rooms. Do you know how it works?
Today, we will design a website chat room. Users enter their usernames to log on to the chat room and adopt the framework structure.
Learning content:
Step 1: Design the homepage and simple counter of the chat room
1. Open VS2008. Create a new website under solution '123' and name it Chatroom. The Default homepage file is Default. aspx.
2. Default. aspx adds a Form Control, switches to the design view, and pulls two Lable controls, one Textbox Control, and one Button control from the left-side toolkit standard group, finally, add a required verification to the Textbox text that enters the nickname.
In the design view, double-click the Btn1 button and write the following Event code in Default. aspx. cs:

Public partial class _ Default: System. web. UI. page {protected void Page_Load (object sender, EventArgs e) {if (Application ["user_online"] = null) {Application ["user_online"] = 0 ;} application ["user_online"] = (int) Application ["user_online"] + 1; Label3.Text = "(currently" + Application ["user_online"]. toString () + "online person !) ";} Protected void button#click (object sender, EventArgs e) {if (Page. isPostBack) {Session ["User_name"] = this. txt1.Text; Response. redirect ("chat. aspx ");}}}

Step 2: Construct a logon string and a speech string
1. Create a Chat. aspx page file. You can use the following HTML language to compile a page program of the framework and divide a window into two halves. The left half window is used to store the Inputwin. aspx page file that inputs the speech content, and the right half window is used to store the Showwin. aspx page file that displays the chat content.
2. Construct a logon message string. Write the following code in the Page_Load event of Chat. aspx. cs:

Protected void Page_Load (object sender, EventArgs e) {string user_name = (string) Session ["user_name"]; string sayStr = "from" + (string) Request. serverVariables ["REMOTE_ADDR"] + "; sayStr = sayStr +" <B> <font color = red> "+ user_name +" </font> </B> "; sayStr = sayStr + "at" + DateTime. now + "coming soon"; Application. lock (); Application ["show"] = sayStr + "<br>" + Application ["show"]; I = I + 1 Application. unLock ();}

3. Construct the speech content string. Create the page file Inputwin. aspx for the input speech content. For page Inputwin. aspx add controls. Here two DropDownList drop-down list box controls are used to select the speaker's Gender and mood, one single-line Textbox Control (to whom ); A multi-line Textbox Control (speech content), a Button (speech Button), and finally add a verification control.
In the design view, double-click the Btn1 button and write the following code in the Btn_click event of the Inputwin. aspx. cs file:

Protected void button#click (object sender, EventArgs e) {if (Page. isPostBack = true) // return page data {String ssex, emotion, who; ssex = DropDownList1.SelectedItem. value; // obtain the Gender emotion = DropDownList2.SelectedItem. text + "; // get the expression who =" to "+" <B> "+ TextBox2.Text +" </B> "; // obtain the target speaker String: string sayStr = "<font size = '3' color = '00ff00'> <B>" + (String) session ["user_name"]; sayStr = sayStr + ssex + "</B> </font> in" + DateTime. now + emotion + who + "said:"; sayStr = sayStr + TextBox3.Text; Application. lock (); Application ["show"] = sayStr + "<br>" + (string) Application ["show"]; Application. unLock (); TextBox3.Text = ""; // clear the dialog box }}

4. Create a page file (Showwin. aspx) that displays the speech string and content. The implementation code is as follows:

<Html xmlns = "http://www.w3.org/1999/xhtml"> 

Write the following code in the Page_Load event of Showwin. aspx. cs:

public partial class showwin : System.Web.UI.Page{  protected void Page_Load(object sender, EventArgs e)  { Response.Write((string)Application["show"]);  }}

5. Write the following code to Exit. aspx. cs on the chat room page:

Protected void Page_Load (object sender, EventArgs e) {string sayStr = "<B>" + (string) Session ["user_name"] + "</B> "; sayStr = sayStr + "at" + DateTime. now + "leaving the chat room"; sayStr = "<font color = 'green'>" + sayStr + "</font>"; Application. lock (); Application ["show"] = sayStr + "<br>" + (string) Application ["show"]; Application ["user_online"] = (int) application ["user_online"]-1; Application. unLock (); Response. redirect ("chatroom. aspx ");}

6. Run the chat room home page Default. aspx.

Run

Summary
Designing a website chat room sounds like you have already entered the website production stage. seeing a very simple chat room designed by yourself has a little sense of accomplishment, although this is only the first step in the Great Wall. Through this study, I learned the respective roles of Session and Application, how to divide the page into two windows, and how to find the correct event for writing. In general, I feel a little bit about web page design, I hope everyone will like it too. Try to design a chat room for your own website.

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.