<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" media="screen" href="/styles/xslt/rss.xslt"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:media="http://search.yahoo.com/mrss/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:c9="http://channel9.msdn.com">
<channel>
	<title>Channel 9</title>
    <atom:link rel="self" type="application/rss+xml" href="http://channel9.msdn.com/Niners/c4f.Tyler-Schlegel/Posts/RSS"></atom:link>
    <itunes:summary></itunes:summary>
    <itunes:author>Microsoft</itunes:author>
    <itunes:subtitle></itunes:subtitle>
    <image>
      <url>http://mschnlnine.vo.llnwd.net/d1/Dev/App_Themes/C9/images/feedimage.png</url>
      <title>Channel 9</title>
      <link>http://channel9.msdn.com/Niners/c4f.Tyler-Schlegel/Posts</link>
    </image>
    <itunes:image href=""></itunes:image>
    <itunes:category text="Technology"></itunes:category>
    <description>Channel 9 keeps you up to date with the latest news and behind the scenes info from Microsoft that developers love to keep up with. From LINQ to SilverLight – Watch videos and hear about all the cool technologies coming and the people behind them.</description>
    <link>http://channel9.msdn.com/Niners/c4f.Tyler-Schlegel/Posts</link>
    <language>en</language>
    <pubDate>Thu, 23 May 2013 10:48:59 GMT</pubDate>
    <lastBuildDate>Thu, 23 May 2013 10:48:59 GMT</lastBuildDate>
    <generator>Rev9</generator>
    <c9:totalResults>1</c9:totalResults>
    <c9:pageCount>1</c9:pageCount>
    <c9:pageSize>25</c9:pageSize>
  <item>
      <title>Basic Chat Using ASP .NET AJAX</title>
      <description><![CDATA[
<p><span id="c4fmetadata">
<table class="" cellspacing="0" cellpadding="1" width="100%" border="0">
<tbody>
<tr class="entry_overview">
<td class="" width="50">&nbsp;</td>
<td class="">
<p><span class="entry_description">The AJAX Chat Sample shows how to build a browser based chat using ASP .NET and AJAX. ASP.NET AJAX is the easiest and most enjoyable way to start writing asynchronous Web applications using ASP.NET. The official ASP.NET AJAX
 site is here, and you will want to make sure you have downloaded the last version of the framework and the control toolkit before continuing with this example.</span></p>
<p><span class="entry_description"></span>&nbsp;</p>
</td>
</tr>
<tr>
<td class="" colspan="2">
<div class="entry_author">Tyler Schlegel</div>
<div class="entry_company"><a href="http://coding4fun.personifydesign.com/Coding4FunAjaxChat.zi"></a></div>
<br>
<div class="entry_details"><b>Difficulty: </b><span class="entry_details_input">Easy</span></div>
<div class="entry_details"><b>Time Required:</b> <span class="entry_details_input">
1-3 hours</span></div>
<div class="entry_details"><b>Cost: </b><span class="entry_details_input">Free</span></div>
<div class="entry_details"><b>Software: </b><span class="entry_details_input"><a href="http://msdn.com/express/">Visual Basic or Visual C# Express Editions</a></span></div>
<div class="entry_details"><b>Hardware: </b><span class="entry_details_input"></span></div>
<div class="entry_details"><b>Download: </b><a class="" href="http://ecn.channel9.msdn.com/o9/c4fcontent/migration/1785048/Coding4FunAjaxChat.zip">Download</a>
<ul>
</ul>
</div>
</td>
</tr>
</tbody>
</table>
</span></p>
<h3>The Code</h3>
<p>Here's an overview of the basic chat logic; it's very simple— </p>
<ol>
<li>A user accesses a page, and a new Chatter object is initialized </li><li>The page class determines which Chats are available in the current application context on the server
</li><li>The page class calls the Chatter's Join method to associate the Chatter with the appropriate Chat object
</li><li>The Chatter begins calling the Chat's SendMessage method to post new messages
</li></ol>
<h5>Create an ASP.NET AJAX-Enabled Web Site and Enable Partial Rendering</h5>
<p>To begin, create a new <a href="http://ajax.asp.net/">ASP.NET AJAX</a>-Enabled Web Site in Visual Studio 2005
</p>
<p><a href="http://ecn.channel9.msdn.com/o9/c4fcontent/migration/1785048/clip_image0022.jpg"><img height="158" src="http://ecn.channel9.msdn.com/o9/c4fcontent/migration/1785048/clip_image0021.jpg" width="240" border="0"></a></p>
<p>&nbsp;</p>
<p>The Default.aspx file created in the new project should contain the following:</p>
<p>&nbsp;</p>
<pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">form</span> <span class="attr">id</span><span class="kwrd">=&quot;form1&quot;</span> <span class="attr">runat</span><span class="kwrd">=&quot;server&quot;</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;</span><span class="html">asp:ScriptManager</span> <span class="attr">ID</span><span class="kwrd">=&quot;ScriptManager1&quot;</span> <span class="attr">runat</span><span class="kwrd">=&quot;server&quot;</span> <span class="kwrd">/&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">form</span><span class="kwrd">&gt;</span></pre>
<pre class="csharpcode"><span class="kwrd"></span>&nbsp;</pre>
<p>Before continuing, add the EnablePartialRendering=&quot;true&quot; attribute to the ScriptManager control. This enables you to update regions of the page individually by using UpdatePanel controls (more on that later!). You will also want to copy the App_Code folder
 from this examples downloadable files to your project directory. The two class files “Chat.cs” and “Chatter.cs” are needed to create our test data.
</p>
<h3>Add Some Chat Controls</h3>
<p>We'll need three controls right away: a BulletedList to show all of the Chatters participating in the Chat, another BulletedList (part of the
<a href="http://www.codeplex.com/Release/ProjectReleases.aspx?ProjectName=AtlasControlToolkit">
control toolkit</a>) to display chat messages (from the server application), and a TextBox to enter new chat messages (from your client browser). You may wonder why we are using a BulletedList to display chat messages rather than a TextBox or ListBox control.
 It would certainly be possible to use either of those alternatives, however a BulletedList allows for more flexibility when styling the HTML output, and by setting the control's CSS overflow style to auto we can create a ListBox-like experience pretty easily.
</p>
<pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">asp:BulletedList</span> <span class="attr">ID</span><span class="kwrd">=&quot;ChattersBulletedList&quot;</span> <span class="attr">runat</span><span class="kwrd">=&quot;server&quot;</span> <span class="kwrd">/&gt;</span>
<span class="kwrd">&lt;</span><span class="html">asp:BulletedList</span> <span class="attr">runat</span><span class="kwrd">=&quot;server&quot;</span> <span class="attr">ID</span><span class="kwrd">=&quot;ChatMessageList&quot;</span> <span class="kwrd">/&gt;</span>
<span class="kwrd">&lt;</span><span class="html">asp:TextBox</span> <span class="attr">ID</span><span class="kwrd">=&quot;NewMessageTextBox&quot;</span> <span class="attr">Columns</span><span class="kwrd">=&quot;50&quot;</span> <span class="attr">runat</span><span class="kwrd">=&quot;server&quot;</span> <span class="kwrd">/&gt;</span>
</pre>
<pre class="csharpcode">We'll also add a button that we will later use to trigger an asynchronous call back to </pre>
<pre class="csharpcode">&nbsp;</pre>
<pre class="csharpcode">Default.aspx:
<span class="kwrd">&lt;</span><span class="html">asp:Button</span> <span class="attr">ID</span><span class="kwrd">=&quot;SendButton&quot;</span> <span class="attr">Text</span><span class="kwrd">=&quot;Send&quot;</span> <span class="attr">runat</span><span class="kwrd">=&quot;server&quot;</span> <span class="attr">OnClick</span><span class="kwrd">=&quot;SendMessage_Click&quot;</span><span class="kwrd">/&gt;</span>
The complete form should look something like this:
<span class="kwrd">&lt;</span><span class="html">form</span> <span class="attr">id</span><span class="kwrd">=&quot;form1&quot;</span> <span class="attr">runat</span><span class="kwrd">=&quot;server&quot;</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;</span><span class="html">asp:ScriptManager</span> <span class="attr">ID</span><span class="kwrd">=&quot;ScriptManager1&quot;</span> <span class="attr">runat</span><span class="kwrd">=&quot;server&quot;</span> <span class="attr">EnablePartialRendering</span><span class="kwrd">=&quot;true&quot;</span> <span class="kwrd">/&gt;</span>
Chatters<span class="kwrd">&lt;</span><span class="html">br</span><span class="kwrd">/&gt;</span>
<span class="kwrd">&lt;</span><span class="html">asp:BulletedList</span> <span class="attr">ID</span><span class="kwrd">=&quot;ChattersBulletedList&quot;</span> <span class="attr">runat</span><span class="kwrd">=&quot;server&quot;</span> <span class="kwrd">/&gt;</span>
Chat Text<span class="kwrd">&lt;</span><span class="html">br</span><span class="kwrd">/&gt;</span>
<span class="kwrd">&lt;</span><span class="html">div</span> <span class="attr">style</span><span class="kwrd">=&quot;width: 640px; height: 240px; overflow: auto;&quot;</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;</span><span class="html">asp:BulletedList</span> <span class="attr">runat</span><span class="kwrd">=&quot;server&quot;</span> <span class="attr">ID</span><span class="kwrd">=&quot;ChatMessageList&quot;</span> <span class="kwrd">/&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">div</span><span class="kwrd">&gt;</span>
Send Message Text<span class="kwrd">&lt;</span><span class="html">br</span><span class="kwrd">/&gt;</span>
<span class="kwrd">&lt;</span><span class="html">asp:TextBox</span> <span class="attr">ID</span><span class="kwrd">=&quot;NewMessageTextBox&quot;</span> <span class="attr">Columns</span><span class="kwrd">=&quot;50&quot;</span> <span class="attr">runat</span><span class="kwrd">=&quot;server&quot;</span> <span class="kwrd">/&gt;&lt;</span><span class="html">asp:Button</span> <span class="attr">ID</span><span class="kwrd">=&quot;SendButton&quot;</span> <span class="attr">Text</span><span class="kwrd">=&quot;Send&quot;</span> <span class="attr">runat</span><span class="kwrd">=&quot;server&quot;</span> <span class="kwrd">/&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">form</span><span class="kwrd">&gt;</span></pre>
<pre class="csharpcode"><span class="kwrd"></span>&nbsp;</pre>
<p>At this point you can run the project and check out the form. </p>
<h5>Add Some Chatters</h5>
<p>In a real-world scenario you would of course implement some kind of user authentication for your application. For the purposes of this example we will skip all of that and simply hard-code our chat application participants in the Application context. To
 do this, add a new Global.asax file to your project by right-clicking the project in the solution tree, clicking “Add New Item” and choosing Global Application Class.
</p>
<p><a href="http://ecn.channel9.msdn.com/o9/c4fcontent/migration/1785048/clip_image00212.jpg"><img height="92" hspace="12" src="http://ecn.channel9.msdn.com/o9/c4fcontent/migration/1785048/clip_image00211.jpg" width="240" border="0"></a>
</p>
<p>Since we want to simulate a group of Chatters that have logged into our site, we will create a list of Chatter objects within the Application_Start method like so—
</p>
<pre class="csharpcode">C#
List&lt;Chatter&gt; chatters = <span class="kwrd">new</span> List&lt;Chatter&gt;();
chatters.Add(<span class="kwrd">new</span> Chatter(<span class="kwrd">new</span> Guid(<span class="str">&quot;CD863C27-2CEE-45fd-A2E0-A69E62B816B9&quot;</span>), <span class="str">&quot;Me&quot;</span>));
chatters.Add(<span class="kwrd">new</span> Chatter(Guid.NewGuid(), <span class="str">&quot;Juan&quot;</span>));
chatters.Add(<span class="kwrd">new</span> Chatter(Guid.NewGuid(), <span class="str">&quot;Joe&quot;</span>));
chatters.Add(<span class="kwrd">new</span> Chatter(Guid.NewGuid(), <span class="str">&quot;Eric&quot;</span>));
chatters.Add(<span class="kwrd">new</span> Chatter(Guid.NewGuid(), <span class="str">&quot;Brian&quot;</span>));
chatters.Add(<span class="kwrd">new</span>     Chatter(Guid.NewGuid(), <span class="str">&quot;Kim&quot;</span>));
chatters.Add(<span class="kwrd">new</span> Chatter(Guid.NewGuid(), <span class="str">&quot;Victor&quot;</span>));
Application.Add(<span class="str">&quot;Chatters&quot;</span>, chatters);</pre>
<pre class="csharpcode"></pre>
<pre class="csharpcode">VB
<span class="kwrd">Dim</span> chatters <span class="kwrd">As</span> List(Of Chatter) = <span class="kwrd">New</span> List(Of Chatter)
chatters.Add(<span class="kwrd">New</span> Chatter(<span class="kwrd">New</span> Guid(<span class="str">&quot;CD863C27-2CEE-45fd-A2E0-A69E62B816B9&quot;</span>), <span class="str">&quot;Me&quot;</span>))
chatters.Add(<span class="kwrd">New</span> Chatter(Guid.NewGuid, <span class="str">&quot;Juan&quot;</span>))
chatters.Add(<span class="kwrd">New</span> Chatter(Guid.NewGuid, <span class="str">&quot;Joe&quot;</span>))
chatters.Add(<span class="kwrd">New</span> Chatter(Guid.NewGuid, <span class="str">&quot;Eric&quot;</span>))
chatters.Add(<span class="kwrd">New</span> Chatter(Guid.NewGuid, <span class="str">&quot;Brian&quot;</span>))
chatters.Add(<span class="kwrd">New</span> Chatter(Guid.NewGuid, <span class="str">&quot;Kim&quot;</span>))
chatters.Add(<span class="kwrd">New</span> Chatter(Guid.NewGuid, <span class="str">&quot;Victor&quot;</span>))
Application.Add(<span class="str">&quot;Chatters&quot;</span>, chatters)</pre>
<pre class="csharpcode">&nbsp;</pre>
<p>(Be sure to add &lt;%@ Import Namespace=&quot;System.Collections.Generic&quot; %&gt; at the top of the file.) This is clearly test-only data, but the import thing to note is that each Chatter gets a new random Guid except for “Me” which gets a hard-coded value. In a real-world
 scenario this would probably be a user identification number stored in a cookie or Session variable, but since we are lazy, we will simply have hard-code it in two places to get a similar effect.
</p>
<p>Next we will create a list of Chat's available in the application (just one for now)—
</p>
<pre class="csharpcode">List<span class="kwrd">&lt;</span><span class="html">Chat</span><span class="kwrd">&gt;</span> chat = new List<span class="kwrd">&lt;</span><span class="html">Chat</span><span class="kwrd">&gt;</span>();
chat.Add(new Chat());
Application.Add(&quot;Chats&quot;, chat);</pre>
<pre class="csharpcode">&nbsp;</pre>
<p>Finally, we will loop through our list of logged-in users and associate them with the single Chat instance added above—
</p>
<pre class="csharpcode">C#
<span class="kwrd">foreach</span> (KeyValuePair&lt;Guid, Chatter&gt; chatter <span class="kwrd">in</span> Chatter.ActiveChatters())
{
chatter.Value.Join(Chat.ActiveChats()[0]);
}
</pre>
<pre class="csharpcode">VB
<span class="kwrd">For</span> <span class="kwrd">Each</span> c <span class="kwrd">As</span> KeyValuePair(Of Guid, Chatter) <span class="kwrd">In</span> Chatter.ActiveChatters
c.Value.Join(Chat.ActiveChats(0))
<span class="kwrd">Next</span></pre>
<pre class="csharpcode"><span class="kwrd"></span>&nbsp;</pre>
<p>The Global.asax code should look something like this—</p>
<p>&nbsp;</p>
<pre class="csharpcode">C#
<span class="kwrd">void</span> Application_Start(<span class="kwrd">object</span> sender, EventArgs e) 
{
List&lt;Chatter&gt; chatters = <span class="kwrd">new</span> List&lt;Chatter&gt;();
chatters.Add(<span class="kwrd">new</span> Chatter(<span class="kwrd">new</span> Guid(<span class="str">&quot;CD863C27-2CEE-45fd-A2E0-A69E62B816B9&quot;</span>), <span class="str">&quot;Me&quot;</span>));
chatters.Add(<span class="kwrd">new</span> Chatter(Guid.NewGuid(), <span class="str">&quot;Juan&quot;</span>));
chatters.Add(<span class="kwrd">new</span> Chatter(Guid.NewGuid(), <span class="str">&quot;Joe&quot;</span>));
chatters.Add(<span class="kwrd">new</span> Chatter(Guid.NewGuid(), <span class="str">&quot;Eric&quot;</span>));
chatters.Add(<span class="kwrd">new</span> Chatter(Guid.NewGuid(), <span class="str">&quot;Brian&quot;</span>));
chatters.Add(<span class="kwrd">new</span> Chatter(Guid.NewGuid(), <span class="str">&quot;Kim&quot;</span>));
chatters.Add(<span class="kwrd">new</span> Chatter(Guid.NewGuid(), <span class="str">&quot;Victor&quot;</span>));
Application.Add(<span class="str">&quot;Chatters&quot;</span>, chatters);

List&lt;Chat&gt; chats = <span class="kwrd">new</span> List&lt;Chat&gt;();
chats.Add(<span class="kwrd">new</span> Chat());
Application.Add(<span class="str">&quot;Chats&quot;</span>, chats);

<span class="kwrd">foreach</span> (KeyValuePair&lt;Guid, Chatter&gt; chatter <span class="kwrd">in</span> Chatter.ActiveChatters())
        {
            chatter.Value.Join(Chat.ActiveChats()[0]);
        }
}</pre>
<pre class="csharpcode"></pre>
<pre class="csharpcode">VB
<span class="kwrd">Sub</span> Application_Start(<span class="kwrd">ByVal</span> sender <span class="kwrd">As</span> <span class="kwrd">Object</span>, <span class="kwrd">ByVal</span> e <span class="kwrd">As</span> EventArgs)
<span class="kwrd">Dim</span> chatters <span class="kwrd">As</span> List(Of Chatter) = <span class="kwrd">New</span> List(Of Chatter)
chatters.Add(<span class="kwrd">New</span> Chatter(<span class="kwrd">New</span> Guid(<span class="str">&quot;CD863C27-2CEE-45fd-A2E0-A69E62B816B9&quot;</span>), <span class="str">&quot;Me&quot;</span>))
chatters.Add(<span class="kwrd">New</span> Chatter(Guid.NewGuid, <span class="str">&quot;Juan&quot;</span>))
chatters.Add(<span class="kwrd">New</span> Chatter(Guid.NewGuid, <span class="str">&quot;Joe&quot;</span>))
chatters.Add(<span class="kwrd">New</span> Chatter(Guid.NewGuid, <span class="str">&quot;Eric&quot;</span>))
chatters.Add(<span class="kwrd">New</span> Chatter(Guid.NewGuid, <span class="str">&quot;Brian&quot;</span>))
chatters.Add(<span class="kwrd">New</span> Chatter(Guid.NewGuid, <span class="str">&quot;Kim&quot;</span>))
chatters.Add(<span class="kwrd">New</span> Chatter(Guid.NewGuid, <span class="str">&quot;Victor&quot;</span>))
Application.Add(<span class="str">&quot;Chatters&quot;</span>, chatters)
<span class="kwrd">Dim</span> chats <span class="kwrd">As</span> List(Of Chat) = <span class="kwrd">New</span> List(Of Chat)
chats.Add(<span class="kwrd">New</span> Chat)
Application.Add(<span class="str">&quot;Chats&quot;</span>, chats)
<span class="kwrd">For</span> <span class="kwrd">Each</span> c <span class="kwrd">As</span> KeyValuePair(Of Guid, Chatter) <span class="kwrd">In</span> Chatter.ActiveChatters
c.Value.Join(Chat.ActiveChats(0))
<span class="kwrd">Next</span>
<span class="kwrd">End</span> <span class="kwrd">Sub</span></pre>
<pre class="csharpcode"><span class="kwrd"></span>&nbsp;</pre>
<p>When the test server first runs our project the code path above will be invoked and we will have a magical list of users in the Application context ready to chat away.
</p>
<h3>Handle Some Events</h3>
<p>Open the code-behind file for Default.aspx (Default.aspx.cs) and add two private variables: one for our Chatter (“Me”), and one for the single Chat instance—
</p>
<pre class="csharpcode"><span class="rem">// You'll notice these are the same values hard-coded in the Global.asax file as well</span>
<span class="kwrd">private</span> Chat m_chat = Chat.ActiveChats()[0];
<span class="kwrd">private</span> Chatter m_chatter = Chatter.ActiveChatters()[<span class="kwrd">new</span> Guid(<span class="str">&quot;CD863C27-2CEE-45fd-A2E0-A69E62B816B9&quot;</span>)];

Next, add a <span class="kwrd">private</span> method called _UpdateChatterList—

<span class="kwrd">private</span> <span class="kwrd">void</span> _UpdateChatterList()
{
ChattersBulletedList.DataSource = m_chat.Chatters;
ChattersBulletedList.DataTextField = <span class="str">&quot;Name&quot;</span>;
ChattersBulletedList.DataBind();
}
</pre>
<pre class="csharpcode">&nbsp;</pre>
<p>Since Chatters will enter and leave the Chat at any time, our global list of Chatters will need to update often. _UpdateChatterList binds our global list of Chatters to the appropriate BulletedList in the Web form every time we post-back to this page. We'll
 also need to update and display the list of messages from our Chat instance. To do this we'll add a new method called _UpdateChatMessageList—
</p>
<pre class="csharpcode">C#
<span class="kwrd">private</span> <span class="kwrd">void</span> _UpdateChatMessageList()
{
ChatMessageList.DataSource = m_chat.Messages;
ChatMessageList.DataBind();
}</pre>
<pre class="csharpcode">VB
<span class="kwrd">Private</span> <span class="kwrd">Sub</span> _UpdateChatMessageList()
ChatMessageList.DataSource = m_chat.Messages
ChatMessageList.DataBind()
<span class="kwrd">End</span> <span class="kwrd">Sub</span></pre>
<pre class="csharpcode"><span class="kwrd"></span>&nbsp;</pre>
<p>Any messages that have been pushed to our Chat instance since the last post-back will now be appended to our Chat message BulletedList.
</p>
<p>The last thing we need to do is handle the button click that occurs when a Chatter sends a new message to the Chat instance. To do this add an OnClick attribute to the “Send” button in Default.aspx—
</p>
<pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">asp:Button</span> <span class="attr">ID</span><span class="kwrd">=&quot;SendButton&quot;</span> <span class="attr">Text</span><span class="kwrd">=&quot;Send&quot;</span> <span class="attr">runat</span><span class="kwrd">=&quot;server&quot;</span> <span class="attr">OnClick</span><span class="kwrd">=&quot;SendButton_Click&quot;</span> <span class="kwrd">/&gt;</span></pre>
<pre class="csharpcode"><span class="kwrd"></span>&nbsp;</pre>
<p>Then, add the event handler for the button click to the code-behind. We'll also call our “Update” methods after sending our chat message—
</p>
<pre class="csharpcode">C#
<span class="kwrd">protected</span> <span class="kwrd">void</span> SendButton_Click(<span class="kwrd">object</span> sender, EventArgs e)
{
<span class="kwrd">if</span> (!<span class="kwrd">string</span>.IsNullOrEmpty(NewMessageTextBox.Text))
{
<span class="kwrd">string</span> messageSent = m_chat.SendMessage(m_chatter, NewMessageTextBox.Text);
}
_UpdateChatterList();
_UpdateChatMessageList();
}</pre>
<pre class="csharpcode"></pre>
<pre class="csharpcode">VB
<span class="kwrd">Protected</span> <span class="kwrd">Sub</span> SendButton_Click(<span class="kwrd">ByVal</span> sender <span class="kwrd">As</span> <span class="kwrd">Object</span>, <span class="kwrd">ByVal</span> e <span class="kwrd">As</span> EventArgs)
<span class="kwrd">If</span> <span class="kwrd">Not</span> <span class="kwrd">String</span>.IsNullOrEmpty(NewMessageTextBox.Text) <span class="kwrd">Then</span>
<span class="kwrd">Dim</span> messageSent <span class="kwrd">As</span> <span class="kwrd">String</span> = m_chat.SendMessage(m_chatter, NewMessageTextBox.Text)
NewMessageTextBox.Text = <span class="kwrd">String</span>.Empty
<span class="kwrd">End</span> <span class="kwrd">If</span>
_UpdateChatterList()
_UpdateChatMessageList()
<span class="kwrd">End</span> <span class="kwrd">Sub</span></pre>
<pre class="csharpcode"><span class="kwrd"></span>&nbsp;</pre>
<p>This will handle the click event from the “Send” button, check if we have any text to send, then call our Chat's SendMessage method which will add the message to our global Chat object.
</p>
<h3>Try It Out!</h3>
<p>Try running the project and sending a few messages. The page will do a complete post-back to the server every time the “Send” button is clicked. Lame. Let's get rid of that by using the ASP.NET AJAX UpdatePanel.&nbsp;
<a href="http://ecn.channel9.msdn.com/o9/c4fcontent/migration/1785048/Coding4FunAjaxChat.zip">Download</a> the sample project.
</p>
<h3>UpdatePanel: The Key to ASP.NET Async Happiness</h3>
<p>Recall that in the first step we added the EnablePartialRendering attribute to the ScriptManager control. Now we can actually use that functionality by wrapping our Chat controls in an UpdatePanel. In the Default.aspx file encapsulate the BulletedList controls
 with an UpdatePanel as follows— </p>
<pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">asp:UpdatePanel</span> <span class="attr">ID</span><span class="kwrd">=&quot;ChatUpdatePanel&quot;</span> <span class="attr">runat</span><span class="kwrd">=&quot;server&quot;</span> <span class="attr">UpdateMode</span><span class="kwrd">=&quot;Always&quot;</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;</span><span class="html">ContentTemplate</span><span class="kwrd">&gt;</span>
Chatters<span class="kwrd">&lt;</span><span class="html">br</span><span class="kwrd">/&gt;</span>
<span class="kwrd">&lt;</span><span class="html">asp:BulletedList</span> <span class="attr">ID</span><span class="kwrd">=&quot;ChattersBulletedList&quot;</span> <span class="attr">runat</span><span class="kwrd">=&quot;server&quot;</span> <span class="kwrd">/&gt;</span>
Chat Text<span class="kwrd">&lt;</span><span class="html">br</span><span class="kwrd">/&gt;</span>
<span class="kwrd">&lt;</span><span class="html">div</span> <span class="attr">style</span><span class="kwrd">=&quot;width: 640px; height: 240px; overflow: auto;&quot;</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;</span><span class="html">asp:BulletedList</span> <span class="attr">runat</span><span class="kwrd">=&quot;server&quot;</span> <span class="attr">ID</span><span class="kwrd">=&quot;ChatMessageList&quot;</span> <span class="kwrd">/&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">div</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">ContentTemplate</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">asp:UpdatePanel</span><span class="kwrd">&gt;</span></pre>
<pre class="csharpcode"><span class="kwrd"></span>&nbsp;</pre>
<p>(Note that you must add a ContentTemplate element to the UpdatePanel before adding the Web controls.)
</p>
<p>To circumvent the normal post-back processing we need to associate this UpdatePanel with a “Trigger,” so after the close of the ContentTemplate element add the following—
</p>
<pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">Triggers</span><span class="kwrd">&gt;</span>

<span class="kwrd">&lt;</span><span class="html">asp:AsyncPostBackTrigger</span> <span class="attr">ControlID</span><span class="kwrd">=&quot;SendButton&quot;</span> <span class="attr">EventName</span><span class="kwrd">=&quot;Click&quot;</span> <span class="kwrd">/&gt;</span>

<span class="kwrd">&lt;/</span><span class="html">Triggers</span><span class="kwrd">&gt;</span>

</pre>
<style type="text/css">
<!--
.csharpcode
	{font-size:small;
	color:black;
	font-family:consolas,"Courier New",courier,monospace;
	background-color:#ffffff}
.csharpcode 
	{font-size:small;
	color:black;
	font-family:consolas,"Courier New",courier,monospace;
	background-color:#ffffff}
.csharpcode 
	{margin:0em}
.csharpcode .rem
	{color:#008000}
.csharpcode .kwrd
	{color:#0000ff}
.csharpcode .str
	{color:#006080}
.csharpcode .op
	{color:#0000c0}
.csharpcode .preproc
	{color:#cc6633}
.csharpcode .asp
	{background-color:#ffff00}
.csharpcode .html
	{color:#800000}
.csharpcode .attr
	{color:#ff0000}
.csharpcode .alt
	{margin:0em;
	width:100%;
	background-color:#f4f4f4}
.csharpcode .lnum
	{color:#606060}
-->
</style>
<p>Now “Send” button clicks will be handled asynchronously, and we don't have to modify any of the synchronous logic we implemented above. Pretty nice!
</p>
<h3>One Last Trigger</h3>
<p>Our chat application doesn't quite work as we'd expect it to yet. We really want the list of messages to update continuously as new messages are sent from other Chatters. We can implement this by adding a new Trigger to the Triggers element, and wiring the
 new Trigger to a Timer control— </p>
<p>... </p>
<pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">asp:AsyncPostBackTrigger</span> <span class="attr">ControlID</span><span class="kwrd">=&quot;ChatTextTimer&quot;</span> <span class="attr">EventName</span><span class="kwrd">=&quot;Tick&quot;</span> <span class="kwrd">/&gt;</span>

<span class="kwrd">&lt;/</span><span class="html">Triggers</span><span class="kwrd">&gt;</span>

</pre>
<style type="text/css">
<!--
.csharpcode
	{font-size:small;
	color:black;
	font-family:consolas,"Courier New",courier,monospace;
	background-color:#ffffff}
.csharpcode 
	{font-size:small;
	color:black;
	font-family:consolas,"Courier New",courier,monospace;
	background-color:#ffffff}
.csharpcode 
	{margin:0em}
.csharpcode .rem
	{color:#008000}
.csharpcode .kwrd
	{color:#0000ff}
.csharpcode .str
	{color:#006080}
.csharpcode .op
	{color:#0000c0}
.csharpcode .preproc
	{color:#cc6633}
.csharpcode .asp
	{background-color:#ffff00}
.csharpcode .html
	{color:#800000}
.csharpcode .attr
	{color:#ff0000}
.csharpcode .alt
	{margin:0em;
	width:100%;
	background-color:#f4f4f4}
.csharpcode .lnum
	{color:#606060}
-->
</style>
<p>Then, add a Timer control to the page. The Interval attribute determines the amount of time between “ticks” of the timer (in milliseconds)—
</p>
<pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">asp:Timer</span> <span class="attr">runat</span><span class="kwrd">=&quot;server&quot;</span> <span class="attr">ID</span><span class="kwrd">=&quot;ChatTextTimer&quot;</span> <span class="attr">Interval</span><span class="kwrd">=&quot;1000&quot;</span> <span class="kwrd">/&gt;</span></pre>
<style type="text/css">
<!--
.csharpcode
	{font-size:small;
	color:black;
	font-family:consolas,"Courier New",courier,monospace;
	background-color:#ffffff}
.csharpcode 
	{font-size:small;
	color:black;
	font-family:consolas,"Courier New",courier,monospace;
	background-color:#ffffff}
.csharpcode 
	{margin:0em}
.csharpcode .rem
	{color:#008000}
.csharpcode .kwrd
	{color:#0000ff}
.csharpcode .str
	{color:#006080}
.csharpcode .op
	{color:#0000c0}
.csharpcode .preproc
	{color:#cc6633}
.csharpcode .asp
	{background-color:#ffff00}
.csharpcode .html
	{color:#800000}
.csharpcode .attr
	{color:#ff0000}
.csharpcode .alt
	{margin:0em;
	width:100%;
	background-color:#f4f4f4}
.csharpcode .lnum
	{color:#606060}
-->
</style>
<p>Finally, add calls to UpdateChatterList and UpdateChatMessageList in the Page_Load event handler. When the timer Tick event fires the page will post-back, the Page_Load handler will be invoked and our message list and Chatter list will update as expected.
 With these changes in place go ahead and run the project again. You may notice that the message list resets its scroll position to the top every time it updates. To fix this add the following JavaScript block after the closing form tag and before the closing
 body tag in Default.aspx— </p>
<pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">script</span> <span class="attr">type</span><span class="kwrd">=&quot;text/javascript&quot;</span><span class="kwrd">&gt;</span>

<span class="kwrd">function</span> _SetChatTextScrollPosition()

{

<span class="kwrd">var</span> chatText = document.getElementById(<span class="str">&quot;ChatText&quot;</span>);

chatText.scrollTop = chatText.scrollHeight;

window.setTimeout(<span class="str">&quot;_SetChatTextScrollPosition()&quot;</span>, 1);

}

window.onload = <span class="kwrd">function</span>()

{

_SetChatTextScrollPosition();

}

<span class="kwrd">&lt;/</span><span class="html">script</span><span class="kwrd">&gt;</span>

</pre>
<style type="text/css">
<!--
.csharpcode
	{font-size:small;
	color:black;
	font-family:consolas,"Courier New",courier,monospace;
	background-color:#ffffff}
.csharpcode 
	{font-size:small;
	color:black;
	font-family:consolas,"Courier New",courier,monospace;
	background-color:#ffffff}
.csharpcode 
	{margin:0em}
.csharpcode .rem
	{color:#008000}
.csharpcode .kwrd
	{color:#0000ff}
.csharpcode .str
	{color:#006080}
.csharpcode .op
	{color:#0000c0}
.csharpcode .preproc
	{color:#cc6633}
.csharpcode .asp
	{background-color:#ffff00}
.csharpcode .html
	{color:#800000}
.csharpcode .attr
	{color:#ff0000}
.csharpcode .alt
	{margin:0em;
	width:100%;
	background-color:#f4f4f4}
.csharpcode .lnum
	{color:#606060}
-->
</style>
<p>The list will flicker slightly as it repositions itself, but the latest message will stay in view at the bottom of the list.
</p>
<p>To test the functionality of your new chat application connect to the test URL with two separate browser windows and type messages into one or the other. You should see your messages pop into the message list of each one no matter which browser sends the
 message. Enjoy! </p>
<h3>Download</h3>
<p><a class="" href="http://ecn.channel9.msdn.com/o9/c4fcontent/migration/1785048/Coding4FunAjaxChat.zip">Download</a> the sample project.
</p>
<h3>Bio</h3>
<p>“Tyler Schlegel is a Software Development Engineer with <a href="http://www.personifydesign.com/">
Personify Design, Inc</a>. He's been developing .NET solutions since 2002.”</p>
<style type="text/css">
<!--
.csharpcode
	{font-size:small;
	color:black;
	font-family:consolas,"Courier New",courier,monospace;
	background-color:#ffffff}
.csharpcode 
	{font-size:small;
	color:black;
	font-family:consolas,"Courier New",courier,monospace;
	background-color:#ffffff}
.csharpcode 
	{margin:0em}
.csharpcode .rem
	{color:#008000}
.csharpcode .kwrd
	{color:#0000ff}
.csharpcode .str
	{color:#006080}
.csharpcode .op
	{color:#0000c0}
.csharpcode .preproc
	{color:#cc6633}
.csharpcode .asp
	{background-color:#ffff00}
.csharpcode .html
	{color:#800000}
.csharpcode .attr
	{color:#ff0000}
.csharpcode .alt
	{margin:0em;
	width:100%;
	background-color:#f4f4f4}
.csharpcode .lnum
	{color:#606060}
-->
</style><style type="text/css">
<!--
.csharpcode
	{font-size:small;
	color:black;
	font-family:consolas,"Courier New",courier,monospace;
	background-color:#ffffff}
.csharpcode 
	{font-size:small;
	color:black;
	font-family:consolas,"Courier New",courier,monospace;
	background-color:#ffffff}
.csharpcode 
	{margin:0em}
.csharpcode .rem
	{color:#008000}
.csharpcode .kwrd
	{color:#0000ff}
.csharpcode .str
	{color:#006080}
.csharpcode .op
	{color:#0000c0}
.csharpcode .preproc
	{color:#cc6633}
.csharpcode .asp
	{background-color:#ffff00}
.csharpcode .html
	{color:#800000}
.csharpcode .attr
	{color:#ff0000}
.csharpcode .alt
	{margin:0em;
	width:100%;
	background-color:#f4f4f4}
.csharpcode .lnum
	{color:#606060}
-->
</style><style type="text/css">
<!--
.csharpcode
	{font-size:small;
	color:black;
	font-family:consolas,"Courier New",courier,monospace;
	background-color:#ffffff}
.csharpcode 
	{font-size:small;
	color:black;
	font-family:consolas,"Courier New",courier,monospace;
	background-color:#ffffff}
.csharpcode 
	{margin:0em}
.csharpcode .rem
	{color:#008000}
.csharpcode .kwrd
	{color:#0000ff}
.csharpcode .str
	{color:#006080}
.csharpcode .op
	{color:#0000c0}
.csharpcode .preproc
	{color:#cc6633}
.csharpcode .asp
	{background-color:#ffff00}
.csharpcode .html
	{color:#800000}
-->
</style> <img src="http://m.webtrends.com/dcs1wotjh10000w0irc493s0e_6x1g/njs.gif?dcssip=channel9.msdn.com&dcsuri=http://channel9.msdn.com/Niners/c4f.Tyler-Schlegel/Posts/RSS&WT.dl=0&WT.entryid=Entry:RSSView:cc85af48c425495286639e7600d57379">]]></description>
      <comments>http://channel9.msdn.com/coding4fun/articles/Basic-Chat-Using-ASP-NET-AJAX</comments>
      <itunes:summary>




&amp;nbsp;

The AJAX Chat Sample shows how to build a browser based chat using ASP .NET and AJAX. ASP.NET AJAX is the easiest and most enjoyable way to start writing asynchronous Web applications using ASP.NET. The official ASP.NET AJAX
 site is here, and you will want to make sure you have downloaded the last version of the framework and the control toolkit before continuing with this example. 
&amp;nbsp; 




Tyler Schlegel


Difficulty: Easy
Time Required: 
1-3 hours
Cost: Free
Software: Visual Basic or Visual C# Express Editions
Hardware: 
Download: Download







 
The Code
Here&#39;s an overview of the basic chat logic; it&#39;s very simple—  

A user accesses a page, and a new Chatter object is initialized The page class determines which Chats are available in the current application context on the server
The page class calls the Chatter&#39;s Join method to associate the Chatter with the appropriate Chat object
The Chatter begins calling the Chat&#39;s SendMessage method to post new messages

Create an ASP.NET AJAX-Enabled Web Site and Enable Partial Rendering
To begin, create a new ASP.NET AJAX-Enabled Web Site in Visual Studio 2005
 
 
&amp;nbsp; 
The Default.aspx file created in the new project should contain the following: 
&amp;nbsp; 
&amp;lt;form id=&amp;quot;form1&amp;quot; runat=&amp;quot;server&amp;quot;&amp;gt;
&amp;lt;asp:ScriptManager ID=&amp;quot;ScriptManager1&amp;quot; runat=&amp;quot;server&amp;quot; /&amp;gt;
&amp;lt;/form&amp;gt;
&amp;nbsp;
Before continuing, add the EnablePartialRendering=&amp;quot;true&amp;quot; attribute to the ScriptManager control. This enables you to update regions of the page individually by using UpdatePanel controls (more on that later!). You will also want to copy the App_Code folder
 from this examples downloadable files to your project directory. The two class files “Chat.cs” and “Chatter.cs” are needed to create our test data.
 
Add Some Chat Controls
We&#39;ll need three controls right away: a BulletedList to show all of the Chatters participating in the Chat, another BulletedList (part of the

control too</itunes:summary>
      <link>http://channel9.msdn.com/coding4fun/articles/Basic-Chat-Using-ASP-NET-AJAX</link>
      <pubDate>Fri, 02 Mar 2007 02:49:00 GMT</pubDate>
      <guid isPermaLink="false">http://channel9.msdn.com/coding4fun/articles/Basic-Chat-Using-ASP-NET-AJAX</guid>
      <media:thumbnail url="http://ecn.channel9.msdn.com/o9/c4f/images/1785048_100.jpg" height="75" width="100"></media:thumbnail>
      <media:thumbnail url="http://ecn.channel9.msdn.com/o9/c4f/images/1785048_220.jpg" height="165" width="220"></media:thumbnail>      
      <dc:creator>Tyler Schlegel</dc:creator>
      <itunes:author>Tyler Schlegel</itunes:author>
      <slash:comments>46</slash:comments>
      <wfw:commentRss>http://channel9.msdn.com/coding4fun/articles/Basic-Chat-Using-ASP-NET-AJAX/RSS</wfw:commentRss>
      <category>Web</category>
    </item>    
</channel>
</rss>