RegisterClientScriptBlock does not work from a server control

C

Cameron Eckman

For some reason I cannot get RegisterClientScriptBlock to render a script
reference on the page from a server control. Yes, I have not placed the code
in the Render method, I also tried the OnInit just in case. Below is the a
simple version of the code, any help is appreciated:

namespace MyCompany.Web
{
public class Header : System.Web.UI.WebControls.WebControl
{

private void RenderClientJavaScript(string javaScriptFilename)
{
string script = string.Format(@"<SCRIPT language=""javascript""
src=""/baxScripts/{0}.js""></SCRIPT>", javaScriptFilename);

if (! Page.IsClientScriptBlockRegistered(javaScriptFilename))
Page.RegisterClientScriptBlock(javaScriptFilename, script);
}

protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
RenderClientJavaScript("basicToolset");
RenderClientJavaScript("webMenu");
}

protected override void Render(HtmlTextWriter writer)
{
writer.Write("Hello World");
}

}
}
 
T

Thysanura

I've tried your code and it works perfectly, look at the HTML rendered by
the page that contains the control and you will see the script tags
 
C

Cameron Eckman

Thanks, your post made me go back and do the simple steps. I've found the
issue I had is that if there is not a <FORM runat=server> tag block on the
page, the script is not written. I didn't read the help deep enough...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top