Ajax.NET Professional and Client Callbacks
Today I tried to use Ajax.NET Professional with Client Callbacks in ASP.NET 2.0. The problem I never used the Client Callbacks was that they are only using strings for the data that is sent between the client and the server. Ajax.NET Professional is sending strings, too. So, why not use Ajax.NET Professional for extended Client Callbacks that allow you to send and receive complex objects instead of strings:
<%@ Page Language="c#" Inherits="System.Web.UI.Page" %>
<%@ Implements Interface="System.Web.UI.ICallbackEventHandler" %>
<script language="c#" runat="server">
void Page_Load(object sender, EventArgs e)
{
string callback = ClientScript.GetCallbackEventReference(this,
"document.getElementById('textBox1').value", "doTest1", null);
textBox1.Attributes.Add("onkeyup", callback);
}
private string eventArg;
void ICallbackEventHandler.RaiseCallbackEvent(string e)
{
eventArg = e;
}
string ICallbackEventHandler.GetCallbackResult()
{
int a = eventArg.Length;
char[] c = new char[a];
for(int i=0; i<a; i++)
{
c[i] = eventArg[i];
}
return AjaxPro.JavaScriptSerializer.Serialize(c);
}
</script>
<html>
<head>
<title>ICallback Example</title>
</head>
<body>
<script type="text/javascript" src="ajaxpro/core.asjx"></script>
<form id="Form1" runat="server">
<asp:TextBox id="textBox1" runat="server" value="Michael"/><br/>
<asp:Label id="label1" runat="server">----</asp:Label>
</form>
<script type="text/javascript">
function doTest1(result, context) {
var html = [];
var res = null;
eval("res = " + result + ";");
for(var i=0; i<res.length; i++) {
html.push(res[i]);
html.push("<br/>");
}
document.getElementById("label1").innerHTML = html.join("");
}
</script>
</body>
</html>
You can see a working demo of the page at http://dotnet2.schwarz-interactive.de/test.aspx.