No output with RegisterClientScriptBlock

  • Thread starter Casper Hornstrup
  • Start date
C

Casper Hornstrup

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
 
S

Steven Cheng[MSFT]

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
 
C

Casper Hornstrup

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();
...
}
 
S

Steven Cheng[MSFT]

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
 
C

Casper Hornstrup

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
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top