No output with RegisterClientScriptBlock

Discussion in 'ASP .Net Web Controls' started by Casper Hornstrup, Jul 7, 2004.

  1. In my webcontrol, I register some javascript once. If I do it like below,
    the script is output.

    private const string HintJavascriptResourceName = "CsiteWeb.Hint.js";

    private const string HintStartupScriptName = "Csite.Web.Hint.js";

    private void RegisterScript()

    {

    if (Page.IsStartupScriptRegistered(HintStartupScriptName))

    {

    return;

    }

    using (Stream rs = System.Reflection.Assembly.GetCallingAssembly().

    GetManifestResourceStream(HintJavascriptResourceName))

    {

    byte[] rsbuf = new byte[rs.Length];

    rs.Read(rsbuf, 0, (int)rs.Length);

    string script = System.Text.Encoding.UTF8.GetString(rsbuf, 0,
    (int)rs.Length);

    Page.RegisterStartupScript(HintStartupScriptName,
    String.Format("<script>{0}\ncsiteHintRegister();</script>", script));

    }

    }



    Now, I have a problem that the script is output a the bottom of the page, so
    sometimes explorer complains that a function is used before it is declared.
    Using IsClientScriptBlockRegistered and RegisterClientScriptBlock instead
    should put the script at the top of the page right? However, the script is
    not output to the page when using IsClientScriptBlockRegistered and
    RegisterClientScriptBlock. How can this be?

    Casper
    Casper Hornstrup, Jul 7, 2004
    #1
    1. Advertising

  2. Hi Casper,

    From your description, you want to use "Page.RegisterClientScriptBlock "
    instead of "Page.RegisterStartupScript"
    to add a certain javascript block in to your ASP.NET web page, but you
    found that the script block didn't appear in the page output ,yes?

    I'm not sure the complete code logic in your code behind, would you provide
    some detailed info, such as when and how you call this "RegisterScript()"
    function in your code behind.

    Also, you can try creating a new page and just use
    "Page.RegisterClientScriptBlock" in the Page_Load to add a simple script
    block to see whether this works.
    In addition, you can add a break point in the "RegisterScript()" function
    and F5 debug the page to see whether the certain code block is entered.

    And here is another tech article on using javascript with asp.net, hope
    also helps:

    #Using JavaScript Along with ASP.NET
    http://msdn.microsoft.com/library/en-us/dnaspp/html/aspnet-usingjavascript.a
    sp?frame=true#aspnet-usingjavascript_topic06

    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)

    Get Preview at ASP.NET whidbey
    http://msdn.microsoft.com/asp.net/whidbey/default.aspx
    Steven Cheng[MSFT], Jul 8, 2004
    #2
    1. Advertising

  3. Hmm yes, when I call RegisterScript could be the problem. I call
    RegisterScript in the Render() method of my web control (this is not a user
    control btw.). Maybe the scripts registered by RegisterClientScriptBlock is
    already output at this time, but the scripts registered by
    RegisterStartupScript is output later? Can I output the javascript from my
    web control at an earlier time? I need to make sure that the script is
    registered only once on a page no matter how many times the web control is
    used on the page. Is RegisterStartupScript/RegisterClientScriptBlock the
    usual method of doing this from a webcontrol?

    protected override void Render(HtmlTextWriter output)
    {
    RegisterScript();
    ...
    }

    "Steven Cheng[MSFT]" <> wrote in message
    news:...

    > Hi Casper,
    >
    > From your description, you want to use "Page.RegisterClientScriptBlock "
    > instead of "Page.RegisterStartupScript"
    > to add a certain javascript block in to your ASP.NET web page, but you
    > found that the script block didn't appear in the page output ,yes?
    >
    > I'm not sure the complete code logic in your code behind, would you

    provide
    > some detailed info, such as when and how you call this "RegisterScript()"
    > function in your code behind.
    >
    > Also, you can try creating a new page and just use
    > "Page.RegisterClientScriptBlock" in the Page_Load to add a simple script
    > block to see whether this works.
    > In addition, you can add a break point in the "RegisterScript()" function
    > and F5 debug the page to see whether the certain code block is entered.
    >
    > And here is another tech article on using javascript with asp.net, hope
    > also helps:
    >
    > #Using JavaScript Along with ASP.NET
    >

    http://msdn.microsoft.com/library/en-us/dnaspp/html/aspnet-usingjavascript.a
    > sp?frame=true#aspnet-usingjavascript_topic06
    >
    > Regards,
    >
    > Steven Cheng
    > Microsoft Online Support
    >
    > Get Secure! www.microsoft.com/security
    > (This posting is provided "AS IS", with no warranties, and confers no
    > rights.)
    >
    > Get Preview at ASP.NET whidbey
    > http://msdn.microsoft.com/asp.net/whidbey/default.aspx
    >
    >
    Casper Hornstrup, Jul 8, 2004
    #3
  4. Hi Casper,

    Thanks for your response. From the code snippet you proivded in the last
    reply, you put the RegisterScript function in the control's Render method.
    I'm not sure whether this is the cause but I suggest you try move the code
    to another event or method which is called before the control's render
    method. Maybe in Load event and test again to see whether the problem
    remains. Also, you can remove the
    if(...) statement before call Page.RegisterScript...
    since it won't register the same script block mulit-times if there is
    already one on page.


    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)

    Get Preview at ASP.NET whidbey
    http://msdn.microsoft.com/asp.net/whidbey/default.aspx
    Steven Cheng[MSFT], Jul 9, 2004
    #4
  5. Hi Cheng.

    Right, this was the problem. The following works with
    RegisterClientScriptBlock:

    override protected void OnInit(EventArgs e)
    {
    RegisterScript();
    base.OnInit(e);
    }

    Thanks for your help.

    Casper Hornstrup
    Csite ApS

    "Steven Cheng[MSFT]" <> wrote in message
    news:...
    > Hi Casper,
    >
    > Thanks for your response. From the code snippet you proivded in the last
    > reply, you put the RegisterScript function in the control's Render method.
    > I'm not sure whether this is the cause but I suggest you try move the

    code
    > to another event or method which is called before the control's render
    > method. Maybe in Load event and test again to see whether the problem
    > remains. Also, you can remove the
    > if(...) statement before call Page.RegisterScript...
    > since it won't register the same script block mulit-times if there is
    > already one on page.
    Casper Hornstrup, Jul 9, 2004
    #5
    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. Brian W
    Replies:
    1
    Views:
    2,906
    Yan-Hong Huang[MSFT]
    Jul 4, 2003
  2. Wes Weems
    Replies:
    1
    Views:
    406
    Ken Cox [Microsoft MVP]
    Jul 19, 2003
  3. John Livermore
    Replies:
    1
    Views:
    523
    Teemu Keiski
    Jul 25, 2003
  4. Scott
    Replies:
    0
    Views:
    396
    Scott
    Jul 27, 2003
  5. MS News
    Replies:
    10
    Views:
    3,094
    MS News \(MS LVP\)
    Aug 6, 2003
Loading...

Share This Page