1 这是一个网页聊天的代码,有一个问题:就是不能显示出发送者的消息:
2 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
3
4 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
6 <html xmlns="http://www.w3.org/1999/xhtml" >
7 <head runat="server">
8 <title>无标题页</title>
9 <link href="style/style.css" type="text/css" rel="stylesheet" />
10
11
12 <script src="js/jquery-1.4.2.min.js"></script>
13 <script type="text/javascript">
14
15 function SetOrGetSayContent()
16 {
17 $.get("SayContent.ashx",
18 function(msg)
19 {
20 var SayContent=msg;
21
22 if($.trim(SayContent)!="")
23 {
24 //这儿考虑过用$(".content").append();方法,但第二次追加html代码时,是覆盖上一次的html代码,html()方法也不行,所以才考虑到用语法自己页面来实现。
25 $.get("SayContentB.ashx?Content="+SayContent);
26 }
27 }
28 );
29
30 setTimeout("SetOrGetSayContent()",1000);
31 }
32
33 </script>
34
35
36
37
38 </head>
39 <body onload="SetOrGetSayContent();">
40 <form id="form1" runat="server">
41 <div class="main">
42 <div id="content" class="content" runat="server">
43
44 </div>
45
46 <div class="say">
47 <asp:TextBox ID="txtMsg" runat="server" TextMode="MultiLine" CssClass="say"></asp:TextBox>
48 </div>
49 <div class="send">
50 <asp:Button ID="btnSendMsg" runat="server" Text="发送" OnClick="btnSendMsg_Click" />
51 </div>
52 </div>
53 <asp:HiddenField ID="HiddenField1" runat="server" />
54
55 </form>
56 </body>
57 </html>
58 Default.aspx.cs代码:
59 using System;
60 using System.Data;
61 using System.Configuration;
62 using System.Web;
63 using System.Web.Security;
64 using System.Web.UI;
65 using System.Web.UI.WebControls;
66 using System.Web.UI.WebControls.WebParts;
67 using System.Web.UI.HtmlControls;
68 using System.IO;
69 using System.Threading;
70 using System.Web.UI.MobileControls;
71 using System.Collections.Generic;
72
73 public partial class _Default : System.Web.UI.Page
74 {
75
76
77 public string MySelfContent = "";
78 protected void Page_Load(object sender, EventArgs e)
79 {
80 if(!IsPostBack)
81 {
82 if(Request.QueryString["content"]!=null)
83 {
84 string contentText = Request.QueryString["content"].ToString();
85 //这里是是显示发送者的消息,就这里并不能显示出发送者的消息,因为asp.net是先执行aspx.cs文件里面的pageLoad方法,然后再加载aspx页面。这里不知道怎么写,希望大家帮个忙,谢谢
86 content.InnerHtml = "<div class=OneMsg>他说:" + contentText + "</div>";
87 }
88 }
89 }
90 protected void btnSendMsg_Click(object sender, EventArgs e)
91 {
92 string SayContent = txtMsg.Text.Trim().ToString();
93
94
95 Application.Remove("Content");
96 Application.Add("Content", SayContent);
97
98 content.InnerHtml+= "<div class=OneMsg>我说:"+SayContent+"</div>";
99 }
100 }
101 SayContent.ashx 处理消息的页面
102 <%@ WebHandler Language="C#" Class="SayContent" %>
103
104 using System;
105 using System.Web;
106 using System.Web.UI.MobileControls;
107 using System.Collections.Generic;
108
109 public class SayContent : IHttpHandler {
110
111
112 public void ProcessRequest (HttpContext context) {
113
114
115 string SayContentTxt = "";
116
117 context.Response.ContentType = "text/plain";
118
119 if (context.Application["Content"] != null)
120 {
121 SayContentTxt = context.Application["Content"].ToString();
122 context.Application.Remove("Content");
123 context.Response.Write(SayContentTxt);
124 context.Response.End();
125 }
126 else
127 {
128 context.Response.Write(SayContentTxt);
129 context.Response.End();
130 }
131 }
132
133 public bool IsReusable {
134 get {
135 return false;
136 }
137 }
138
139 }