This is a migrated thread and some comments may be shown as answers.

RadScriptBlock: How to Programatically add Javascript

2 Answers 454 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
ADe
Top achievements
Rank 1
ADe asked on 20 Sep 2008, 02:35 PM
My website needs to output some dynamically generated Javascript but it doesn't run when I AJAX up my controls. I assume because the script element is not connected to the AJAX Manager/Panel.

So I added a RadScriptBlock but I can't for the life of me find out how to add the script to it from the c# server side.

Is this possible?

The last thing I tried was this (but Text is obviously not right),

LiteralControl script = (LiteralControl)radScriptBlock.Controls[0];  
script.Text = "UpdateMainWindow('" + Session["IdM_btnUpdate_ClientID"].ToString() + "');"; 

2 Answers, 1 is accepted

Sort by
0
ADe
Top achievements
Rank 1
answered on 20 Sep 2008, 03:44 PM
OK, I figured this out (but still have a problem).

The following adds the script to the RadScriptBlock and it works fine unless I try and AJAX up my page.

LiteralControl script = new LiteralControl("<script type=\"text/javascript\" language=\"javascript\">ShowSiteLASelector('" + Session["IdM_radMenuContext_ClientID"].ToString() + "');</script>");  
radScriptBlock.Controls.Clear();  
radScriptBlock.Controls.Add(script); 

The problem I still have is that when my RadScriptBlock is put within a RadAjaxPanel then the javascript fails to execute. Any idea why...?

ADe
0
Sebastian
Telerik team
answered on 23 Sep 2008, 01:54 PM
Hi ADe,

Do you add the script to the RadScriptBlock control programmatically each time the PageLoad handler of the page is executed? You may also directly modify the LiteralControl nested in the RadScriptBlock instance as follows:

String scriptContent = "script type=\"text/javascript\" language=\"javascript\">ShowSiteLASelector('" + Session["IdM_radMenuContext_ClientID"].ToString() + "');</script>";     
(radScriptBlock.Controls[0] as LiteralControl).Text = scriptContent;     
 
 

Best regards,
Stephen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Ajax
Asked by
ADe
Top achievements
Rank 1
Answers by
ADe
Top achievements
Rank 1
Sebastian
Telerik team
Share this question
or