Click here to Skip to main content
6,630,289 members and growing! (22,886 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate

Simple Chat asp.net + Ajax

By dealarconjose

this describes how to do a Autorefresh Chat container using a div ajax asp.net
VB, Javascript, XML, HTML, Windows, .NET, ASP.NET, Visual Studio, Ajax, Dev
Posted:12 Dec 2005
Views:58,462
Bookmarked:25 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
11 votes for this article.
Popularity: 1.93 Rating: 1.86 out of 5
6 votes, 54.5%
1

2
1 vote, 9.1%
3

4
4 votes, 36.4%
5

Sample screenshot

Introduction

This article describe how to make a chat without Applet or other heavy component.

i'll Ajax to refresh ONLY a <div> container .

The messages will be stored in a application Object. Ajax will creates a javascript proxy for the server�s functions.

you can test the application opening two Internet Explorer and sending messages from each to other

Adding Ajax.dll to the project

-copy and paste Ajax.dll free library into bin directory

you can download from http://ajax.schwarz-interactive.de/csharpsample/default.aspx

Manual:

http://ajax.schwarz-interactive.de/download/AjaxGuide.doc

- Add this reference to the project

- add this to web.config it�s necesary to works with Ajax

<httpHandlers>
<add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax" />
</httpHandlers>
- add this references to .aspx page
<script language="javascript" src="ajax/common.ashx"></script> 
<script language="javascript" src="ajax/SimpleChat.chat,SimpleChat.ashx">
</script> 
<script language="javascript" src="ajax/common.ashx"></script>
<script language="javascript" src="ajax/SimpleChat.chat,SimpleChat.ashx">
</script>

SimpleChat-->name of namenspace (Project)

chat-->name of the class of code behind

the .aspx page

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="chat.aspx.vb" 
         Inherits="SimpleChat.chat"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" 
      content="http://schemas.microsoft.com/intellisense/ie5">
<link rel="stylesheet" type="text/css" href="Styles.css">
<script language="javascript" src="ajax/common.ashx"></script>
<script language="javascript" src="ajax/SimpleChat.chat,SimpleChat.ashx">
</script>

<script language="javascript">
var cadenaMsg
function dameMsg_CallBack(response){ 
if (response.error != null){ 
document.getElementById('d').innerHTML=(response.error); 
return; }
document.getElementById('d').innerHTML=response.value;
setTimeout("chat.dameMsg(dameMsg_CallBack)",3000);
}
</script>
</HEAD>
<body onload="chat.dameMsg(dameMsg_CallBack);">
<form id="Form1" method="post" runat="server">
<div id="clanci">
<h2><a href="#">Chat Privado</a></h2>

<div id='d' style="border:1px solid #000; width:250px;height:150px;overflow: 
auto;padding:5px 5px 3px 3px;" ></div>

Texto: <input type="text" id="txtMsg" name="txtMsg" class="texto" size="50">

<input type="button" class="boton" 
  onclick="javascript:chat.enviaMsg(document.getElementById('txtMsg').value);
           document.getElementById('txtMsg').value='';" value="Enviar"> 
</div>
</form>
</body>
</HTML>

Code Behind

Imports Ajax
Public Class chat

Inherits System.Web.UI.Page

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load

  'Introducir aqu� el c�digo de usuario para inicializar la p�gina

  If Application.Item("strChat") Is Nothing Then
      Application.Add("strChat", "")
  End If
End Sub

<Ajax.AjaxMethod(HttpSessionStateRequirement.ReadWrite)> _
  Public Function dameMsg() As String
  Dim cadena As String = ""
  If HttpContext.Current.Application("msgChat") Is Nothing Then

HttpContext.Current.Application("msgChat") = "Bienvenidos:<br>" Else ' HttpContext.Current.Application("msgChat") += Now.ToLongTimeString '+ "<br>" cadena = HttpContext.Current.Application("msgChat") If cadena.Length > 5000 Then HttpContext.Current.Application("msgChat") = cadena.Remove(0, 2000) End If End If Return cadena End Function <Ajax.AjaxMethod(HttpSessionStateRequirement.ReadWrite)> _ Public Function enviaMsg(ByVal texto As String) If HttpContext.Current.Application("msgChat") Is Nothing Then HttpContext.Current.Application("msgChat") = texto Else HttpContext.Current.Application("msgChat") += texto + " hora:" + _ Now.ToLongTimeString + "<br>" End If End Function End Class

note:function with Ajax.AjaxMethod can be called from javascript in the client side.

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

About the Author

dealarconjose


Member

Occupation: Web Developer
Location: Spain Spain

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 16 of 16 (Total in Forum: 16) (Refresh)FirstPrevNext
Generalajax-enabled GridView PinmemberJoaoSilva200716:34 29 May '07  
GeneralTurn ASP.NET controls into AJAXenabled controls PinmemberOmegaCD21:46 8 Jul '06  
GeneralAjax enabled DataGrid - Chat Application Example PinmemberOmegaCD3:56 26 May '06  
General'chat' undefined PinmemberAmorous6:13 15 May '06  
QuestionRe: 'chat' undefined PinmemberEhsan Abbasi23:11 28 Aug '06  
AnswerRe: 'chat' undefined Pinmemberdealarconjose8:46 29 Aug '06  
GeneralRe: 'chat' undefined PinmemberLvX740:02 4 Mar '08  
NewszumiPage for ASP.NET PinmemberAmir L.14:04 21 Dec '05  
GeneralRe: zumiPage for ASP.NET Pinmemberdealarconjose14:37 21 Dec '05  
GeneralRe: zumiPage for ASP.NET PinmemberAmir L.22:17 21 Dec '05  
Questionno picture PinmemberAdelino Araújo14:03 13 Dec '05  
AnswerRe: no picture Pinmemberdealarconjose22:26 13 Dec '05  
GeneralRe: no picture PinmemberAdelino Araújo8:32 14 Dec '05  
GeneralRe: no picture-->source+image Pinmemberdealarconjose13:33 14 Dec '05  
GeneralRe: no picture-->source+image PinmemberAmorous9:56 13 May '06  
GeneralRe: no picture-->source+image Pinmemberdealarconjose13:33 15 May '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 12 Dec 2005
Editor:
Copyright 2005 by dealarconjose
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project