Asp. NET Web chat room design and implementation (3rd section) _ Practical skills

Source: Internet
Author: User

Everybody has played the website chat room, that knows how it realizes?
Today we will design a website chat room, the user enters the user name to log in the chat room, uses the frame structure realization.
Learning content:
first step, chat room home and simple counter design
1, open VS2008. Under Solution ' 101, create a new Web site named chatroom. The default first file is Default.aspx.
2. Add form controls for Default.aspx, switch to Design view, drag out 2 lable controls from the left Toolbox Standard Group, 1 textbox controls, a button control, and finally add required validation to the TextBox text for which you entered the nickname.
Double-click the Btn1 button in Design view to 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) 
 { C5/>if (application["user_online"] = = null) 
 { 
 application["user_online"] = 0; 
 } 
 application["User_online"] = (int) application["User_online"] + 1; 
 Label3.text = "(now altogether" + application["User_online"]. ToString () + "people online!)"; 
} 
 protected void Button1_Click (object sender, EventArgs e) 
{ 
 if (page.ispostback) 
 { 
 session["User_" Name "]=this. Txt1.text; 
 Response.Redirect ("chat.aspx");}}

Step two, build the login string with the speech string
1, create chat.aspx page files, use the following HTML language can write a framework page program, a window into two halves. The left half of the window is used to store the paging file inputwin.aspx, and the right half window is used to hold the paging file showwin.aspx to display the chat content.
2, build the login message string. Write the following code in the Chat.aspx.cs Page_Load event:

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 + "on" + DateTime.Now + "Welcome";

 Application.Lock ();
 Application["show"] = Saystr + "<br>" + application["show"];i=i+1
 Application.UnLock ();
 



3, build the speech content string. Create a paging file inputwin.aspx to enter your speech. Adds a control to the page inputwin.aspx, which uses two DropDownList Drop-down list box controls to select the speaker's gender and mood, one single-line TextBox control (to whom); a multiline TextBox control (speech content) ; a button buttons (speaking button), and the validation control is added finally.
Double-click the Btn1 button in Design view to write the code in the Btn_click event of the Inputwin.aspx.cs file as follows:

protected void Button1_Click (object sender, EventArgs e)
 {
 if (Page.IsPostBack = true)//page data postback
 {
 String Ssex, emotion, who;
 Ssex = DropDownList1.SelectedItem.Value; Get the sex
 emotion = DropDownList2.SelectedItem.Text + "";//Get the speech expression who 
 = "to" + "<b>" + TextBox2.Text + "</ B> "; Get to who say
 //Build speech 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 = "";//Empty Speech box
 }
 }

4. Create a paging file (showwin.aspx) that displays the speech string and speech content, implementing the following code:

 
 

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

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

5, to leave the Chat room page Exit.aspx.cs file to write code as follows:

protected void Page_Load (object sender, EventArgs e)
 {
 string saystr = "<b>" + (String) session["user_name" ] + "</b>";
 Saystr = Saystr + "on" + DateTime.Now + "Leave 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 Default.aspx.

Run Effect chart

Summarize
The design website chat room, sounded like is already entered to the website production this link, saw oneself design a function very simple chat room, has a little achievement feeling, although this is only the Great Wall's first step. Through this learning to understand the session and application of their respective roles, learned how to make the page divided into two windows, how to find the right event to write, the overall design of the web has some feeling, I hope everyone can like, try to design a own website chat room bar

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.