Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to design and realize the chat room of ASP.NET website

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to design and implement ASP.NET website chat room". In daily operation, I believe many people have doubts about how to design and implement ASP.NET website chat room. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to design and implement ASP.NET website chat room". Next, please follow the editor to study!

The first step is to design the home page of chat room and simple counter.

1. Open VS2008. Under solution '101', create a new Web site and name it Chatroom. The default home file is Default.aspx.

2, add a form control for Default.aspx, switch to "Design" view, drag 2 Lable controls, 1 Textbox control, and 1 Button control from the standard group of the toolbox on the left, and finally add required verification to the Textbox text of the input nickname.

Double-click the Btn1 button in Design view 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 = "(now there is a total of" 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");}

The second step is to build the login string and the speech string

1, create Chat.aspx page file, use the following HTML language can write sub-frame page program, divide a window into two halves. The left half window is used to store the page file Inputwin.aspx of the input speech content, and the right half window is used to store the page file Showwin.aspx that displays the chat content.

2. Build the login 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 +"+ user_name +"; sayStr = sayStr +" in "+ DateTime.Now +" coming "; Application.Lock (); Application [" show "] = sayStr +"

"+ Application [" show "]; I=I+1 Application.UnLock ();}

3. Construct the speech content string. Create a paging file Inputwin.aspx to enter the content of the speech. Add controls to the page Inputwin.aspx, where two DropDownList drop-down list box controls are used to select the speaker's gender and mood, a single-line Textbox control (to whom), a multi-line Textbox control (speaking content), a Button button (speaking button), and finally adding a validation control.

Double-click the Btn1 button in Design view and write the following code in the Btn_click event of the Inputwin.aspx.cs file:

Protected void Button1_Click (object sender, EventArgs e) {if (Page.IsPostBack = = true) / / Page data return {String ssex, emotion, who; ssex = DropDownList1.SelectedItem.Value; / / get gender emotion = DropDownList2.SelectedItem.Text + "s"; / / get the emoji who = "pair" + "+ TextBox2.Text +"; / / get to whom to speak / / build the speech string: String sayStr = "" + (string) Session ["user_name"] SayStr = sayStr + ssex + "in" + DateTime.Now + emotion + who + "say:"; sayStr = sayStr + TextBox3.Text; Application.Lock (); Application ["show"] = sayStr + "

"+ (string) Application [" show "]; Application.UnLock (); TextBox3.Text ="; / / clear the floor box}}

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

Untitled page

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 code for the Exit.aspx.cs file leaving the chat room page as follows:

Protected void Page_Load (object sender, EventArgs e) {string sayStr = "" + (string) Session ["user_name"] + "; sayStr = sayStr +" in "+ DateTime.Now +" left the chat room "; sayStr =" + sayStr + "; Application.Lock (); Application [" show "] = sayStr +"

"+ (string) Application [" show "]; Application [" user_online "] = (int) Application [" user_online "]-1; Application.UnLock (); Response.Redirect (" chatroom.aspx ");}

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

Running effect diagram

At this point, the study on "how to design and implement ASP.NET website chat room" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report