RegisterClientScriptBlock does not work from a server control

Discussion in 'ASP .Net Web Controls' started by Cameron Eckman, Mar 3, 2005.

  1. 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");
    }

    }
    }
    Cameron Eckman, Mar 3, 2005
    #1
    1. Advertising

  2. Cameron Eckman

    Thysanura Guest

    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




    "Cameron Eckman" <Cameron > escribió en el
    mensaje news:...
    > 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");
    > }
    >
    > }
    > }
    >
    Thysanura, Mar 3, 2005
    #2
    1. Advertising

  3. 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...


    "Thysanura" wrote:

    > 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
    >
    >
    >
    >
    > "Cameron Eckman" <Cameron > escribió en el
    > mensaje news:...
    > > 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");
    > > }
    > >
    > > }
    > > }
    > >

    >
    >
    >
    Cameron Eckman, Mar 3, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Livermore
    Replies:
    1
    Views:
    536
    Teemu Keiski
    Jul 25, 2003
  2. Big Dave
    Replies:
    0
    Views:
    2,310
    Big Dave
    Dec 2, 2004
  3. Simon Harris
    Replies:
    1
    Views:
    619
    Simon Harris
    Jul 28, 2005
  4. =?Utf-8?B?UGF1bCBSZWVk?=
    Replies:
    8
    Views:
    642
    =?Utf-8?B?UGF1bCBSZWVk?=
    Nov 5, 2007
  5. Cameron Eckman

    RegisterClientScriptBlock does not work from a server control

    Cameron Eckman, Mar 3, 2005, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    161
Loading...

Share This Page