Initial AJAX JavaScript objects

Michael Schwarz on Thursday, October 6, 2005

In some scenarios you will call an AJAX method direct after the window.onload event. The window.onload event will start an asyncronous method and wait for the response. As you can imagine the call will need some milliseconds to get the data back from the server. If you want to have the data available when the page first renderes you can add following code to you Page_Load event.

protected void Page_Load(object sender, EventArgs e) { StringBuilder sb = new StringBuilder(); sb.Append("<script type=\"text/javascript\">"); sb.Append("var initialData = ");

AjaxPro.JSON.JSONParser json = new AjaxPro.JSON.JSONParser(); sb.Append(json.GetJSONString(GetData()));

sb.Append(";"); sb.Append("</script>");

Controls.Add(new LiteralControl(sb.ToString())); }

[AjaxPro.AjaxMethod] public DataSet GetData() { // ... }

The method GetData will be invoked from time to time from the JavaScript code without a postback or refresh. Use methods on both part, server-side and client-side source code.