Click here to Skip to main content
11,414,561 members (68,368 online)
Click here to Skip to main content

Simple chat application for ASP.NET

, 26 May 2004
Rate this:
Please Sign up or sign in to vote.
Very easy Web application of a chat room for Internet Explorer 5+ in ASP.NET.

Sample Image - SimpleChat.jpg

Introduction

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.

Background

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;
            pArray.Add(sAddText);

            if ( pArray.Count > 200 )
            {
                pArray.RemoveRange(0,10);
            }
        }

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

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

            return(sResponse);
        }

        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)
                return("");

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

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

            return(sRet);
        }
    }

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"] = 
                       Request.Params["Channel"].ToString();
                else
                    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.
            //
            InitializeComponent();
            base.OnInit(e);
        }
        
        /// <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);

        }
        #endregion

        public string GetChatPage()
        {
            return("TheChatScreenWin.aspx");
        }

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

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

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

            
            if ( TB_ToSend.Text.Length >

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.

License

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

Share

About the Author

Albert Pascual
Web Developer
United States United States
Al is just another Software Engineer working in C++, ASp.NET and C#. Enjoys snowboarding in Big Bear, and wait patiently for his daughters to be old enough to write code and snowboard.

Al is a Microsoft ASP.NET MVP

Blog

Comments and Discussions

 
GeneralMy vote of 5 Pin
Abhijeet Desai 101212-Jun-13 3:43
memberAbhijeet Desai 101212-Jun-13 3:43 
GeneralThis is the simplest chating application.. Pin
souvickcse29-Mar-13 11:11
membersouvickcse29-Mar-13 11:11 
GeneralRe: This is the simplest chating application.. Pin
Member 944699323-Jul-13 23:14
memberMember 944699323-Jul-13 23:14 
BugGetting Error Pin
Tarunshah18-Mar-13 20:34
memberTarunshah18-Mar-13 20:34 
GeneralMy vote of 5 Pin
shani_75930-Dec-12 22:03
membershani_75930-Dec-12 22:03 
Questionits good Pin
sibinkara19-Oct-12 5:07
membersibinkara19-Oct-12 5:07 
Generalits good to use. [modified] Pin
N V Saran30-Aug-12 3:51
memberN V Saran30-Aug-12 3:51 
QuestionCode of Online chat using ASP.NET with C#.NET Pin
Member 874882828-Jul-12 22:27
memberMember 874882828-Jul-12 22:27 
GeneralMy vote of 1 Pin
Member 819871726-Jul-12 21:20
memberMember 819871726-Jul-12 21:20 
GeneralThanks alot Pin
Member 872528513-Mar-12 9:14
memberMember 872528513-Mar-12 9:14 
Generalchat application Pin
dhanalakshmis24-Feb-12 23:07
memberdhanalakshmis24-Feb-12 23:07 
Generalmy vote of 5 Pin
Uday P.Singh7-Dec-11 2:07
memberUday P.Singh7-Dec-11 2:07 
GeneralMy vote of 1 Pin
biabandapela13-Jun-11 23:21
memberbiabandapela13-Jun-11 23:21 
GeneralMy vote of 5 Pin
GAiXz30-May-11 19:02
memberGAiXz30-May-11 19:02 
Generalhi Pin
kabir14316-Apr-11 3:46
memberkabir14316-Apr-11 3:46 
GeneralMy vote of 5 Pin
onkarsaggi16-Dec-10 1:37
memberonkarsaggi16-Dec-10 1:37 
GeneralMy vote of 3 Pin
shanawazway14-Oct-10 2:29
membershanawazway14-Oct-10 2:29 
GeneralMy vote of 5 Pin
prabut22-Sep-10 0:03
memberprabut22-Sep-10 0:03 
GeneralVisual studio 2010 version with full forms based authentication Pin
Patrick Smith10-Jun-10 5:53
memberPatrick Smith10-Jun-10 5:53 
GeneralRe: Visual studio 2010 version with full forms based authentication Pin
Member 441891012-Feb-11 7:47
memberMember 441891012-Feb-11 7:47 
GeneralRe: Visual studio 2010 version with full forms based authentication Pin
Patrick Smith13-Feb-11 10:07
memberPatrick Smith13-Feb-11 10:07 
Generalchaat Pin
kalpesh borse8-Feb-10 9:52
memberkalpesh borse8-Feb-10 9:52 
GeneralRe: chaat Pin
larshittu15-Apr-10 22:43
memberlarshittu15-Apr-10 22:43 
GeneralNot Single Author Uploading The Real Application Code Pin
ajay_zenta14-Aug-09 7:56
memberajay_zenta14-Aug-09 7:56 
GeneralChat Application Pin
shangi_123-Jul-09 3:01
membershangi_123-Jul-09 3:01 
Generalmultiple user Pin
mani_ascent7-Jan-09 3:52
membermani_ascent7-Jan-09 3:52 
GeneralRe: multiple user Pin
Aswani from Hyderabad Andhra Pradesh, India15-Nov-11 0:20
groupAswani from Hyderabad Andhra Pradesh, India15-Nov-11 0:20 
Generalthank u Pin
phaninderp12-Oct-08 4:23
memberphaninderp12-Oct-08 4:23 
Generalhi Pin
RAHULPANDARE17-Sep-08 20:57
memberRAHULPANDARE17-Sep-08 20:57 
Generalhai Pin
Member 447717428-Aug-08 21:15
memberMember 447717428-Aug-08 21:15 
Generalhai i want to add multiple chat rooms Pin
Member 447717428-Aug-08 21:26
memberMember 447717428-Aug-08 21:26 
QuestionHow to add the new chat rooms.. Pin
desigank26-Aug-08 20:47
memberdesigank26-Aug-08 20:47 
AnswerRe: How to add the new chat rooms.. Pin
JaydeepBhatia22-Mar-12 18:05
memberJaydeepBhatia22-Mar-12 18:05 
QuestionChat Feature Pin
er.piyushverma073-Jul-08 3:13
memberer.piyushverma073-Jul-08 3:13 
Generalusing database Pin
digdem3-Jun-08 11:01
memberdigdem3-Jun-08 11:01 
GeneralRe: using database Pin
desigank26-Aug-08 20:50
memberdesigank26-Aug-08 20:50 
GeneralSimJax.. Pin
beodd17-Mar-08 9:44
memberbeodd17-Mar-08 9:44 
Questioninternal search engine code Pin
umesh12345623-Oct-07 0:31
memberumesh12345623-Oct-07 0:31 
Generalonline chat Pin
neha mathur5-Jul-07 8:04
memberneha mathur5-Jul-07 8:04 
Generali want simple chat application in asp.net using vb Pin
karthikeyan_caleb16-Apr-07 20:05
memberkarthikeyan_caleb16-Apr-07 20:05 
Generalplease... Pin
choorakkuttyil22-Mar-07 22:03
memberchoorakkuttyil22-Mar-07 22:03 
GeneralRe: please... Pin
Moh.Shafe3i31-Mar-08 2:10
memberMoh.Shafe3i31-Mar-08 2:10 
GeneralRe: please... Pin
HarshadKamble7-May-11 8:25
memberHarshadKamble7-May-11 8:25 
Generalusing chat On PCs connected with Lan Pin
niting8520-Mar-07 20:00
memberniting8520-Mar-07 20:00 
Generalabout chat Pin
rajakvl200419-Dec-06 21:40
memberrajakvl200419-Dec-06 21:40 
Generalworking with vwd2005 chat not in C# code Pin
bernie_01124-Oct-06 21:18
memberbernie_01124-Oct-06 21:18 
GeneralHi Albert Pascual, I have 2 doubts Pin
rajasekarbtech12-Sep-06 4:47
memberrajasekarbtech12-Sep-06 4:47 
GeneralRe: Hi Albert Pascual, I have 2 doubts Pin
littleheartzzz19-Dec-06 2:56
memberlittleheartzzz19-Dec-06 2:56 
Questionhi Pin
renatofixer21-Aug-06 14:02
memberrenatofixer21-Aug-06 14:02 
Questionhow to show that the user is online Pin
jasine7-Aug-06 18:33
memberjasine7-Aug-06 18:33 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150427.4 | Last Updated 27 May 2004
Article Copyright 2004 by Albert Pascual
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid