Click here to Skip to main content
13,596,321 members
Click here to Skip to main content
Add your own
alternative version


32 bookmarked
Posted 12 Dec 2005

Simple Chat + Ajax

, 12 Dec 2005
Rate this:
Please Sign up or sign in to vote.
this describes how to do a Autorefresh Chat container using a div ajax

Sample Image - SimpleChat.jpg


And why not, how to create an easy chat room for your web site? Well, the best way is to use a nice database to store messages; however, for demo purposes, I'll use a static array. I know, you won't be able to use it in your web farm. Take this article as the concept, not as a solution. This simple web chat program is intended to work in any browser supporting <iFrame>.

Also, you can select multiple chat rooms. Why not extend from there and more from channel to channel.


Some months ago, I was looking for a complete on-line customer service ASP.NET control to make my life easier, did not find anything interesting, so I built my own.

Using the code

Replace this class if you are using a database to save the messages:

public class Chat
        static protected ArrayList pArray = new ArrayList();

        static public void AddMessage(string sDealer, 
                              string sUser, string sMsg)
            string sAddText = sDealer + "~" + sUser + "~" + sMsg;

            if ( pArray.Count > 200 )

        static public string GetAllMessages(string sDealer)
            string sResponse = "";

            for (int i=0; i< pArray.Count; i++)
                sResponse = sResponse + 
                    FormatChat(pArray[i].ToString(), sDealer);


        static private string FormatChat(string sLine, string sDealer)
            int iFirst = sLine.IndexOf("~");
            int iLast = sLine.LastIndexOf("~");

            string sDeal = sLine.Substring(0, iFirst);
            if ( sDeal != sDealer)

            string sUser = sLine.Substring(iFirst+1, iLast-(iFirst+1));
            string sMsg = sLine.Substring(iLast+1);

            string sRet = "" + sUser + ": " + sMsg + "";


The above code reads and writes from the static array like in a database. The code only allows having 200 messages in the array, after that it deletes the top 10 at the time.

The Chat page is pretty simple; this is the code behind aspx.cs:

public class ChatWin : System.Web.UI.Page
        protected System.Web.UI.WebControls.TextBox TB_ToSend;
        protected System.Web.UI.WebControls.Button BT_Send;
        private void Page_Load(object sender, System.EventArgs e)
            if ( Page.IsPostBack == false )
                if ( Request.Params["Channel"] != null )
                    Session["ChatChannel"] = 
                    Session["ChatChannel"] = "1";

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
        /// <span class="code-SummaryComment"><SUMMARY></span>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// <span class="code-SummaryComment"></SUMMARY></span>
        private void InitializeComponent()
            this.BT_Send.Click += 
               new System.EventHandler(this.BT_Send_Click);
            this.Load += new System.EventHandler(this.Page_Load);


        public string GetChatPage()

        private void BT_Send_Click(object sender, System.EventArgs e)
            string sChannel = "";
            string sUser = "";

            if ( Request.Params["Channel"] != null )
                sChannel = Request.Params["Channel"].ToString();
                sChannel = "1";

            if ( Request.Params["User"] != null )
                sUser = Request.Params["User"].ToString();
                Random pRan = new Random();
                int iNum = pRan.Next(9);
                sUser = "Annonymouse" + iNum;

            if ( TB_ToSend.Text.Length > 0)
                TB_ToSend.Text = "";        

When the SEND button is clicked, it calls the function AddMessage that adds a row into the end of the static array.

The page inside the <iframe> tag refreshes every 4 seconds without refreshing your actual page.

Points of Interest

The magic? None, a simple request of the second page into the <iFrame>. So, Internet Explorer takes care of everything for us to read the static array.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
Spain Spain
No Biography provided

You may also be interested in...

Comments and Discussions

Generalajax-enabled GridView Pin
JoaoSilva200729-May-07 15:34
memberJoaoSilva200729-May-07 15:34 
GeneralTurn ASP.NET controls into AJAXenabled controls Pin
OmegaCD8-Jul-06 20:46
memberOmegaCD8-Jul-06 20:46 
GeneralAjax enabled DataGrid - Chat Application Example Pin
OmegaCD26-May-06 2:56
memberOmegaCD26-May-06 2:56 
General'chat' undefined Pin
Amorous15-May-06 5:13
memberAmorous15-May-06 5:13 
QuestionRe: 'chat' undefined Pin
Ehsan Abbasi28-Aug-06 22:11
memberEhsan Abbasi28-Aug-06 22:11 
AnswerRe: 'chat' undefined Pin
dealarconjose29-Aug-06 7:46
memberdealarconjose29-Aug-06 7:46 
GeneralRe: 'chat' undefined Pin
LvX743-Mar-08 23:02
memberLvX743-Mar-08 23:02 
NewszumiPage for ASP.NET Pin
Amir L.21-Dec-05 13:04
memberAmir L.21-Dec-05 13:04 
GeneralRe: zumiPage for ASP.NET Pin
dealarconjose21-Dec-05 13:37
memberdealarconjose21-Dec-05 13:37 
GeneralRe: zumiPage for ASP.NET Pin
Amir L.21-Dec-05 21:17
memberAmir L.21-Dec-05 21:17 
Questionno picture Pin
Adelino Araújo13-Dec-05 13:03
memberAdelino Araújo13-Dec-05 13:03 
AnswerRe: no picture Pin
dealarconjose13-Dec-05 21:26
memberdealarconjose13-Dec-05 21:26 
GeneralRe: no picture Pin
Adelino Araújo14-Dec-05 7:32
memberAdelino Araújo14-Dec-05 7:32 
GeneralRe: no picture--&gt;source+image Pin
dealarconjose14-Dec-05 12:33
memberdealarconjose14-Dec-05 12:33 
GeneralRe: no picture--&gt;source+image Pin
Amorous13-May-06 8:56
memberAmorous13-May-06 8:56 
GeneralRe: no picture--&gt;source+image Pin
dealarconjose15-May-06 12:33
memberdealarconjose15-May-06 12:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03-2016 | 2.8.180621.3 | Last Updated 12 Dec 2005
Article Copyright 2005 by dealarconjose
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid