registerclientscriptblock error

W

wardy1975

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
 
W

wardy1975

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();");
 
L

lisa

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
 
S

Sergey Poberezovskiy

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
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top