Telerik RadAjax offers the ability to execute custom JavaScript code which comes as a response from the server thus giving you more flexibility to complete more specific or complex tasks on the client.
The best and most intuitive approach is to use the ResponseScripts property of the RadAjaxPanel or RadAjaxManager.
Here is a source code example which pops an alert when a Button is clicked:
| C# |
Copy Code |
|
private void Button1_Click(object sender, System.EventArgs e) { AjaxManager1.ResponseScripts.Add(string.Format("alert('Hello from the server! Server time is {0}');", DateTime.Now.ToLongTimeString())); } |
Another approach, however, is to simply add a literal control to the
Controls collection of the panel which contains the necessary script:
| C# |
Copy Code |
|
string script = "<script type='text/javascript'>alert('hi');</script>"; AjaxPanel1.Controls.Add(new LiteralControl(script)); |
For cases when you have code blocks in the .aspx you might encounter "Cannot modify controls collection .." error. In this case, place an asp:PlaceHolder in the AJAX Panel and add the Literal to its Controls collection instead:
| C# |
Copy Code |
|
string script = "<script type='text/javascript'>alert('hi');</script>"; PlaceHolder1.Controls.Add(new LiteralControl(script)); |
You can also check this KB article out.