Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > RegisterClientScriptBlock does not work from a server control

Reply
Thread Tools

RegisterClientScriptBlock does not work from a server control

 
 
Cameron Eckman
Guest
Posts: n/a
 
      03-03-2005
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(javaScriptFilen ame))
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");
}

}
}

 
Reply With Quote
 
 
 
 
Thysanura
Guest
Posts: n/a
 
      03-03-2005
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 http://www.velocityreviews.com/forums/(E-Mail Removed)> escribió en el
mensaje news:(E-Mail Removed)...
> 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(javaScriptFilen ame))
> 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");
> }
>
> }
> }
>



 
Reply With Quote
 
 
 
 
Cameron Eckman
Guest
Posts: n/a
 
      03-03-2005
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 (E-Mail Removed)> escribió en el
> mensaje news:(E-Mail Removed)...
> > 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(javaScriptFilen ame))
> > 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");
> > }
> >
> > }
> > }
> >

>
>
>

 
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
Does Page.RegisterClientScriptBlock Not Work Under ASP.NET 2.0? =?Utf-8?B?UGF1bCBSZWVk?= ASP .Net 8 11-05-2007 06:01 PM
Composite Web Control - RegisterClientScriptBlock Not Written Simon Harris ASP .Net 1 07-28-2005 07:31 PM
RegisterClientScriptBlock does not work from a server control Cameron Eckman ASP .Net Building Controls 1 03-05-2005 01:24 PM
RegisterClientScriptBlock not working from server control. Help? Big Dave ASP .Net 0 12-02-2004 03:04 PM
control equivalent to Page.RegisterClientScriptBlock? John Livermore ASP .Net 1 07-25-2003 01:44 PM



Advertisments