Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > No output with RegisterClientScriptBlock

Reply
Thread Tools

No output with RegisterClientScriptBlock

 
 
Casper Hornstrup
Guest
Posts: n/a
 
      07-07-2004
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(HintStartupScriptN ame))

{

return;

}

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

GetManifestResourceStream(HintJavascriptResourceNa me))

{

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


 
Reply With Quote
 
 
 
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      07-08-2004
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...ngjavascript.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


 
Reply With Quote
 
 
 
 
Casper Hornstrup
Guest
Posts: n/a
 
      07-08-2004
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]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

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



 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      07-09-2004
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



 
Reply With Quote
 
Casper Hornstrup
Guest
Posts: n/a
 
      07-09-2004
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]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Where is RegisterClientScriptBlock?? RegisterScripts etc. MS News ASP .Net 10 08-06-2003 02:24 AM
Styles not being applied with RegisterClientScriptBlock Scott ASP .Net 0 07-27-2003 12:23 AM
control equivalent to Page.RegisterClientScriptBlock? John Livermore ASP .Net 1 07-25-2003 01:44 PM
RegisterClientScriptBlock to register OUTSIDE form tags Wes Weems ASP .Net 1 07-19-2003 07:04 PM
Re: RegisterClientScriptBlock & RegisterStartupScript revisited Brian W ASP .Net 1 07-04-2003 01:43 AM



Advertisments