VS.NET - ASP.NET 2.0 - A Simple Script Callback example written in VB.Net

Synopsis:

ASP.NET 2.0 introduced and AJAX like feature called Script Callbacks. This allows a webpage to execute serverside code and update a webpage without refreshing the entire screen. Listed below is my first Script Callback project. It simply returns the server time or available server memory on a button click without a full refresh of the page. While the functionality of this example is relatively useless, I think the code serves as a good first example at Script Callbacks using VB.net. For information on Script Callbacks:
More info from Microsoft:I believe there may be some bugs in the vb.net code listed here
Some more info from Microsoft
Atlas,AJAX and script CallBack blog post

Solution:

MarkUpCode

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="CallBackSample" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Sample Script Callback</title>
    <script type="text/javascript">
function ReceiveServerData(arg, context)
{
       document.getElementById('lblResults').innerText = arg;
}
</script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
         <input id="Button1" type="button" value="Server Time" onclick="CallServer('time','')" />
        <input id="Button2" type="button" value="Server Memory" onclick="CallServer('mem','')" /><br />
        <br />
        <asp:Label ID="lblResults" runat="server" BorderColor="Maroon" BorderWidth="1px" ForeColor="Red"
            Width="232px"></asp:Label><br />
      </div>
    </form>
</body>
</html>

Code Behind

Partial Class CallBackSample
    Inherits System.Web.UI.Page
    Implements ICallbackEventHandler

    Private _callbackArg As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim cm As ClientScriptManager = Page.ClientScript
        Dim cbReference As String
        cbReference = cm.GetCallbackEventReference(Me, "arg", _
            "ReceiveServerData", "context")
        Dim callbackScript As String
        callbackScript = "function CallServer(arg, context)" & _
            "{" & cbReference & "; }"
        cm.RegisterClientScriptBlock(Me.GetType(), _
            "CallServer", callbackScript, True)
    End Sub


    Public Sub RaiseCallbackEvent(ByVal eventArgument As String) _
     Implements System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent
        _callbackArg = eventArgument
    End Sub

    Public Function GetCallbackResult() As String _
     Implements System.Web.UI.ICallbackEventHandler.GetCallbackResult
        Select Case _callbackArg
            Case "time"
                Return My.Computer.Clock.LocalTime.ToString()
            Case "mem"
                Return (My.Computer.Info.AvailablePhysicalMemory / 1024).ToString() & "K"
            Case Else
                Return _callbackArg
        End Select

    End Function
     
End Class
About this page: