Your Ad Here
Google

Monday, March 05, 2007

 

Simple Chat asp.net + AJAX - how to do a Autorefresh Chat container using a div AJAX asp.net

Introduction

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

i'll Ajax to refresh ONLY a 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




- add this references to .aspx page




SimpleChat-->name of namenspace (Project)

chat-->name of the class of code behind

the .aspx page
























Code Behind
Collapse
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

_
Public Function dameMsg() As String
Dim cadena As String = ""
If HttpContext.Current.Application("msgChat") Is Nothing Then
HttpContext.Current.Application("msgChat") = "Bienvenidos:
"
Else
' HttpContext.Current.Application("msgChat") += Now.ToLongTimeString
'+ "
"

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

_
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 + "
"
End If
End Function

End Class

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

Labels:


Comments: Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?