Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net Building Controls (http://www.velocityreviews.com/forums/f59-asp-net-building-controls.html)
-   -   registerclientscriptblock error (http://www.velocityreviews.com/forums/t757667-registerclientscriptblock-error.html)

wardy1975@yahoo.com 04-27-2005 01:41 PM

registerclientscriptblock error
 
I'm currently trying to register a block of javascript on a custom web
control using the Page.RegisterClientScriptBlock functionality. I am
trying to include the "<!--" in the javascript block to hide it as
required by XHTML standards, but when I add these characters and
attempt to register the block, the block is no longer seen by the page
(I get an object required error). If, however, I remove the hide
characters, the block executes properly. Everything works fine, also,
if I manually enter the script block in the html of the control, but it
fails again if I attempt to add the block to a literal.

Anyone ever run into this problem before?

Thanks


lisa@starways.net 04-27-2005 04:19 PM

Re: registerclientscriptblock error
 
Can you post the code?

Lisa

wardy1...@yahoo.com wrote:
> I'm currently trying to register a block of javascript on a custom

web
> control using the Page.RegisterClientScriptBlock functionality. I am
> trying to include the "<!--" in the javascript block to hide it as
> required by XHTML standards, but when I add these characters and
> attempt to register the block, the block is no longer seen by the

page
> (I get an object required error). If, however, I remove the hide
> characters, the block executes properly. Everything works fine,

also,
> if I manually enter the script block in the html of the control, but

it
> fails again if I attempt to add the block to a literal.
>
> Anyone ever run into this problem before?
>
> Thanks



wardy1975@yahoo.com 04-27-2005 06:02 PM

Re: registerclientscriptblock error
 
Yup...here it is:

StringBuilder sb = new StringBuilder();
sb.Append("<SCRIPT TYPE='text/javascript' LANGUAGE='JavaScript'>");
sb.Append("<!--");
sb.Append("function handleKP() {");
sb.Append( "if (event.keyCode=='13') {");
if (this.imgGo.Visible)
{
sb.Append( "return __doPostBack('_ctl56$imgGo','');");
}
else if (this.hlGo.Visible)
{
sb.Append( "return __doPostBack('_ctl56$hlGo','');");
}
else
{
sb.Append( "return __doPostBack('_ctl56$btnGo','');");
}
sb.Append( "}");
sb.Append("}");
sb.Append("//-->");
sb.Append("</script>");

Page.RegisterClientScriptBlock("scriptSearch", sb.ToString());

this.txtSearchInput.Attributes.Add("onKeyUp", "handleKP();");


lisa@starways.net 04-27-2005 06:41 PM

Re: registerclientscriptblock error
 
Did you try running it in debug to see what the value of sb.ToString is
before it registers the block?

Also, with a block of client script, you generally want to check to
make sure that it isn't inserted more than once.

If Not Page.IsClientScriptBlockRegistered("ListBoxPlus_js ") Then
Dim script As String = "<script language='javascript'
type='text/javascript' >" _
+ ControlChars.CrLf _
+ "<!--" _
+ ControlChars.CrLf _
+ "A whole bunch of script" _
+ ControlChars.CrLf _
+ "//-->" _
+ ControlChars.CrLf _
+ "</script>"
Page.RegisterClientScriptBlock("ListBoxPlus_js", script)
script = Nothing
End If

This is what I'm using in one of my controls, and it works just fine.
But regardless, you probably want to check the value of sb.ToString
before it gets written. It may not be coming out as you think it is.
Creating the script on the fly also creates that possibility.

Lisa


wardy1...@yahoo.com wrote:
> Yup...here it is:
>
> StringBuilder sb = new StringBuilder();
> sb.Append("<SCRIPT TYPE='text/javascript' LANGUAGE='JavaScript'>");
> sb.Append("<!--");
> sb.Append("function handleKP() {");
> sb.Append( "if (event.keyCode=='13') {");
> if (this.imgGo.Visible)
> {
> sb.Append( "return __doPostBack('_ctl56$imgGo','');");
> }
> else if (this.hlGo.Visible)
> {
> sb.Append( "return __doPostBack('_ctl56$hlGo','');");
> }
> else
> {
> sb.Append( "return __doPostBack('_ctl56$btnGo','');");
> }
> sb.Append( "}");
> sb.Append("}");
> sb.Append("//-->");
> sb.Append("</script>");
>
> Page.RegisterClientScriptBlock("scriptSearch", sb.ToString());
>
> this.txtSearchInput.Attributes.Add("onKeyUp", "handleKP();");



Sergey Poberezovskiy 04-27-2005 08:58 PM

Re: registerclientscriptblock error
 
That is too simple:
you forgot to put ControlChars.CrLf between the opening
comment and the function declaration:
>-----Original Message-----
>Yup...here it is:
>
>StringBuilder sb = new StringBuilder();
>sb.Append("<SCRIPT TYPE='text/javascript'

LANGUAGE='JavaScript'>");
>sb.Append("<!--");

IN_ HERE: put CrLf
>sb.Append("function handleKP() {");
>sb.Append( "if (event.keyCode=='13') {");
>if (this.imgGo.Visible)
>{
>sb.Append( "return __doPostBack('_ctl56$imgGo','');");
>}
>else if (this.hlGo.Visible)
>{
>sb.Append( "return __doPostBack('_ctl56$hlGo','');");
>}
>else
>{
>sb.Append( "return __doPostBack('_ctl56$btnGo','');");
>}
>sb.Append( "}");
>sb.Append("}");

SAME_HERE: separate the two lines
>sb.Append("//-->");
>sb.Append("</script>");
>
>Page.RegisterClientScriptBlock("scriptSearch", sb.ToString

());
>
>this.txtSearchInput.Attributes.Add("onKeyUp", "handleKP

();");
>
>.
>


wardy1975@yahoo.com 04-28-2005 04:10 PM

Re: registerclientscriptblock error
 
Hi Lisa and Sergey....thanks for the help. That fixed the problem.



All times are GMT. The time now is 12:46 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.