registerclientscriptblock error

Discussion in 'ASP .Net Building Controls' started by wardy1975@yahoo.com, Apr 27, 2005.

  1. Guest

    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
    , Apr 27, 2005
    #1
    1. Advertising

  2. Guest

    Can you post the code?

    Lisa

    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
    , Apr 27, 2005
    #2
    1. Advertising

  3. Guest

    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();");
    , Apr 27, 2005
    #3
  4. Guest

    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


    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();");
    , Apr 27, 2005
    #4
  5. 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

    ();");
    >
    >.
    >
    Sergey Poberezovskiy, Apr 27, 2005
    #5
  6. Guest

    Hi Lisa and Sergey....thanks for the help. That fixed the problem.
    , Apr 28, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian W
    Replies:
    1
    Views:
    2,907
    Yan-Hong Huang[MSFT]
    Jul 4, 2003
  2. Wes Weems
    Replies:
    1
    Views:
    407
    Ken Cox [Microsoft MVP]
    Jul 19, 2003
  3. John Livermore
    Replies:
    1
    Views:
    524
    Teemu Keiski
    Jul 25, 2003
  4. Steven K

    Error using RegisterClientScriptBlock

    Steven K, Apr 14, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    363
    =?Utf-8?B?QmluIFNvbmcsIE1DUA==?=
    Apr 14, 2004
  5. MW
    Replies:
    3
    Views:
    459
Loading...

Share This Page