如何設(shè)計(jì)與實(shí)現(xiàn)ASP.NET網(wǎng)站聊天室-創(chuàng)新互聯(lián)

這篇文章主要介紹“如何設(shè)計(jì)與實(shí)現(xiàn)ASP.NET網(wǎng)站聊天室”,在日常操作中,相信很多人在如何設(shè)計(jì)與實(shí)現(xiàn)ASP.NET網(wǎng)站聊天室問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”如何設(shè)計(jì)與實(shí)現(xiàn)ASP.NET網(wǎng)站聊天室”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!

建網(wǎng)站原本是網(wǎng)站策劃師、網(wǎng)絡(luò)程序員、網(wǎng)頁(yè)設(shè)計(jì)師等,應(yīng)用各種網(wǎng)絡(luò)程序開(kāi)發(fā)技術(shù)和網(wǎng)頁(yè)設(shè)計(jì)技術(shù)配合操作的協(xié)同工作。創(chuàng)新互聯(lián)公司專業(yè)提供網(wǎng)站制作、成都網(wǎng)站建設(shè),網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站制作(企業(yè)站、響應(yīng)式網(wǎng)站、電商門戶網(wǎng)站)等服務(wù),從網(wǎng)站深度策劃、搜索引擎友好度優(yōu)化到用戶體驗(yàn)的提升,我們力求做到極致!

第一步,聊天室首頁(yè)與簡(jiǎn)單計(jì)數(shù)器設(shè)計(jì)
1、打開(kāi)VS2008。在“解決方案‘101'下新建網(wǎng)站,命名為Chatroom.默認(rèn)首頁(yè)文件為Default.aspx。
2、為Default.aspx添加窗體控件,切換到“設(shè)計(jì)”視圖,從左側(cè)工具箱標(biāo)準(zhǔn)組中拖出2個(gè)Lable控件,1個(gè)Textbox控件,一個(gè)Button控件,最后給輸入昵稱的Textbox文本添加必填驗(yàn)證。
在“設(shè)計(jì)”視圖中雙擊Btn1按鈕,在Default.aspx.cs中編寫(xiě)如下事件代碼:


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 = "(現(xiàn)在共有" + Application["user_online"].ToString() + "人在線!)"; 
} 
 protected void Button1_Click(object sender, EventArgs e) 
{ 
 if (Page.IsPostBack) 
 { 
 Session["User_name"]=this.Txt1.Text; 
 Response.Redirect("chat.aspx");

 
 } 
 }
}

第二步,構(gòu)建登錄字符串與發(fā)言字符串
1、創(chuàng)建Chat.aspx頁(yè)面文件,使用如下HTML語(yǔ)言可以編寫(xiě)分框架頁(yè)面程序,把一個(gè)窗口分成兩半。左半窗口用來(lái)存放輸入發(fā)言內(nèi)容的頁(yè)面文件Inputwin.aspx,右半窗口用來(lái)存放顯示聊天內(nèi)容的頁(yè)面文件Showwin.aspx。
2、構(gòu)建登錄消息字符串。在Chat.aspx.cs的Page_Load事件中編寫(xiě)代碼如下:


protected void Page_Load(object sender, EventArgs e)
 {
 string user_name = (string)Session["user_name"];
 string sayStr = "來(lái)自" + (string)Request.ServerVariables["REMOTE_ADDR"] + "的";
 sayStr = sayStr + "<b><font color=red>" + user_name + "</font></b>";
 sayStr = sayStr + "于" + DateTime.Now + "大駕光臨";

 Application.Lock();
 Application["show"] = sayStr + "<br>" + Application["show"];I=I+1
 Application.UnLock();
 }

3、構(gòu)建發(fā)言內(nèi)容字符串。創(chuàng)建輸入發(fā)言內(nèi)容的頁(yè)面文件Inputwin.aspx。為頁(yè)面Inputwin.aspx添加控件,這里使用兩個(gè)DropDownList下拉列表框控件,分別用來(lái)選擇發(fā)言人的性別和心情,一個(gè)單行Textbox控件(對(duì)誰(shuí)說(shuō));一個(gè)多行Textbox控件(發(fā)言內(nèi)容);一個(gè)Button按鈕(發(fā)言按鈕),最后添加驗(yàn)證控件。
在“設(shè)計(jì)”視圖中雙擊Btn1(發(fā)言)按鈕,在Inputwin.aspx.cs文件的Btn_click事件中編寫(xiě)代碼如下:


protected void Button1_Click(object sender, EventArgs e)
 {
 if (Page.IsPostBack == true) //頁(yè)面數(shù)據(jù)回傳
 {
 String ssex, emotion, who;
 ssex = DropDownList1.SelectedItem.Value; //獲取性別
 emotion = DropDownList2.SelectedItem.Text + "的"; //獲取發(fā)言時(shí)表情 
 who = "對(duì)" + "<b>" + TextBox2.Text + "</b>"; //獲取對(duì)誰(shuí)說(shuō)
 //構(gòu)建發(fā)言字符串:
 String sayStr = "<font size='3' color='00ff00'><b>" + (string)Session["user_name"];
 sayStr = sayStr + ssex + "</b></font>在" + DateTime.Now + emotion + who + " 說(shuō):";
 sayStr = sayStr + TextBox3.Text;
 Application.Lock();
 Application["show"] = sayStr + "<br>" + (string)Application["show"];
 Application.UnLock();
 TextBox3.Text = "";// 將發(fā)言框清空
 }
 }

4、創(chuàng)建顯示發(fā)言字符串和發(fā)言內(nèi)容的頁(yè)面文件(Showwin.aspx),實(shí)現(xiàn)代碼如下:


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <title>無(wú)標(biāo)題頁(yè)</title>
 <meta http-equiv="refresh" content="4"/>
</head>
<body>
 <form id="form1" runat="server">
 <div>
 </div>
 </form>
</body>
</html>

在Showwin.aspx.cs的Page_Load事件中編寫(xiě)代碼如下:


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

如何設(shè)計(jì)與實(shí)現(xiàn)ASP.NET網(wǎng)站聊天室

5、為離開(kāi)聊天室頁(yè)面的Exit.aspx.cs文件編寫(xiě)代碼如下:


protected void Page_Load(object sender, EventArgs e)
 {
 string sayStr = "<b>" + (string)Session["user_name"] + "</b>";
 sayStr = sayStr + "于" + DateTime.Now + "離開(kāi)聊天室了";
 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、運(yùn)行聊天室首頁(yè)Default.aspx。

運(yùn)行效果圖

如何設(shè)計(jì)與實(shí)現(xiàn)ASP.NET網(wǎng)站聊天室

到此,關(guān)于“如何設(shè)計(jì)與實(shí)現(xiàn)ASP.NET網(wǎng)站聊天室”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!

文章標(biāo)題:如何設(shè)計(jì)與實(shí)現(xiàn)ASP.NET網(wǎng)站聊天室-創(chuàng)新互聯(lián)
分享路徑:http://muchs.cn/article28/djigjp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、全網(wǎng)營(yíng)銷推廣、搜索引擎優(yōu)化、App開(kāi)發(fā)、企業(yè)網(wǎng)站制作外貿(mào)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁(yè)設(shè)計(jì)