asp.net 聊天程序

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="chat.aspx.cs" Inherits="chat" ValidateRequest="false" EnableEventValidation="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function ScrollContent() {
            var lastContent = "";
            setInterval(function () {
                var content = document.getElementById("Content");
                if (content) {
                    if (content.innerHTML != lastContent) {
                        content.scrollTop = content.scrollHeight;
                        lastContent = content.innerHTML;
                    }
                }
            }, 1000);
        }
        ScrollContent();
    </script>
</head>
<body style="margin:0;padding:0;">
    <form id="form1" runat="server" style="margin:0 auto;">
    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
    <table cellspacing="5px" style="width:500px;height:500px;background-color:#DDE2E8;border:0px solid #4F53BC;margin:0 auto;padding:0;">
        <tr>
            <td style="background:white;border:1px solid #BDBDC2;vertical-align:top;margin:0;padding:0;">
                <div style="width:100%;height:300px;margin:0;padding:0;border-width:0px;font-weight:normal;font-size:14px;overflow:auto;" id="Content">
                    <div style="color:black;padding:5px;">
                        <img src="Images/bubbleSpeech.png" alt="" />&nbsp;Never give out your password or credit card number in an instant message conversation.
                    </div>
                    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
                    <ContentTemplate>
                    <asp:Repeater ID="rptContents" runat="server">
                        <ItemTemplate>
                    <table width="100%" style="padding-left:5px;margin-bottom:0px;" cellspacing="0" cellpadding="0">
                        <tr>
                            <td style="color:#B2859C;"><%#Eval("UserName") %></td>
                            <td style="color:#B2859C;padding-left:10px;"><%# Convert.ToDateTime(Eval("AddDate")).ToString("yyyy-MM-dd HH:mm") %></td>
                        </tr>
                        <tr>
                            <td style="padding-left:5px;padding-top:2px;margin:0;" colspan="2">
                                <textarea cols="" rows="" style="border-width:0px;width:100%;height:100%;overflow:hidden;border-style:none;resize:none;margin:0;padding:0;"
                                    onfocus="window.activeobj=this;this.clock=setInterval(function(){activeobj.style.height=activeobj.scrollHeight+'px';},200);"
                                    onblur="clearInterval(this.clock);"
                                    readonly="readonly"><%#Eval("Content") %></textarea>
                            </td>
                        </tr>
                    </table>
                        </ItemTemplate>
                    </asp:Repeater>
                    </ContentTemplate>
                    </asp:UpdatePanel>
                    <asp:UpdatePanel ID="UpdatePanel3" runat="server" UpdateMode="Conditional">
                    <ContentTemplate>
                    <asp:Timer runat="server" ID="Timer1" Interval="2000" Enabled="true" ontick="Timer1_Tick"></asp:Timer>
                    </ContentTemplate>
                    </asp:UpdatePanel>
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <asp:UpdatePanel ID="UpdatePanel2" runat="server" ChildrenAsTriggers="True" UpdateMode="Conditional">
                <ContentTemplate>
                <table cellpadding="0" cellspacing="0">
                    <tr>
                        <td style="background:white;border:1px solid #BDBDC2;vertical-align:top;">
                            <textarea runat="server" id="txtSend" rows="1" cols="1" style="border-width:0px;width:480px;height:150px;margin:0;padding:0;border-width:0px;" title="Alt+S to send"></textarea>
                        </td>
                    </tr>
                    <tr>
                        <td style="border:0px solid #BDBDC2;vertical-align:middle;height:25px;margin:0;padding-top:5px;text-align:right;">
                            <asp:Button ID="btnSend" runat="server" Text="  Send  " 
                                onclick="btnSend_Click" />
                        </td>
                    </tr>
                </table>
                </ContentTemplate>
                </asp:UpdatePanel>
            </td>
        </tr>
    </table>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class chat : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["mode"] == null || Request.QueryString["mode"] != "windows")
        {
            Response.ClearContent();
            Response.Write("<script type=\"text/javascript\">\n");
            Response.Write("window.open(\"chat.aspx?mode=windows\", \"\", \"height=500px ,width=500px,menubar=no,resizable=no,toolbar=no,titlebar=no,status=no,top=\" + (window.screen.height - 500)/2 + \",left=\" + (window.screen.width - 500)/2 );\n");
            Response.Write("window.opener=null;");
            Response.Write("window.open(\"\",\"_self\");");
            Response.Write("window.close();");
            Response.Write("</script>\n");
            Response.End();
        }
    }

    protected void btnSend_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(txtSend.Value))
            return;

        var record = new Record();
        record.Content = Server.HtmlEncode(txtSend.Value);
        record.AddDate = DateTime.Now;
        record.UserName = User.Identity.Name;
        AddContent(record);

        txtSend.Value = string.Empty;
    }

    private void AddContent(Record record)
    {
        var contents = Application["ChatRecords"] as List<Record>;
        if (contents == null)
        {
            Application["ChatRecords"] = new List<Record> { record };
        }
        else
        {
            contents.Add(record);
            Application["ChatRecords"] = contents;
        }
    }

    private List<Record> GetAllContent()
    {
        var records = Application["ChatRecords"] as List<Record>;
        if (records != null)
            return records;

        return new List<Record>();
    }

    protected void Timer1_Tick(object sender, EventArgs e)
    {
        try
        {
            var records = GetAllContent();
            if (Session["LastCount"] != null)
            {
                var lastCount = Convert.ToInt32(Session["LastCount"]);
                if (lastCount == records.Count)
                    return;
            }
            Session["LastCount"] = records.Count;
            rptContents.DataSource = records;
            rptContents.DataBind();
            UpdatePanel1.Update();
        }
        catch (Exception)
        {

        }
        
    }
}

public class Record
{
    public string UserName { get; set; }
    public DateTime AddDate { get; set; }
    public string Content { get; set; }
}

 

posted on 2013-02-02 13:41  空明流光  阅读(244)  评论(0编辑  收藏  举报

导航