Tuesday, September 8, 2009

Call JQuery code from ASP.Net server side

A little trick to call JQuery code from the server side.
A typical example is that when one wants to show different panels of a wizard or when a form is submitted successfully, one wants to show 'Congratulations' page.

Basically one just needs to inject the Javascript code in the server side code and JQuery will execute it once it is in control.
Like the following code snippet:

StringBuilder
sb = new StringBuilder();
sb.AppendLine("$(document).ready(function() {");

sb.AppendLine("showForm();");
sb.AppendLine(" });");

Page.ClientScript.RegisterClientScriptBlock(typeof(Page),Guid.NewGuid().ToString(), sb.ToString(), true);

Sidenote, the JQuery document ready function can be in an external javascript file. The above will still work.

No comments:

Post a Comment