1

I can't seem to add some javascript to a StringBuilder. I have a working page which generates HTML content in the form of a div tag. This works fine. I just wanted to add a simple script at the end of the StringBuilder so the client side gets an alert when the div tag finishes appearing on the screen, i.e:

objStringBuilder.Append("<script language='javascript'>")
objStringBuilder.Append("alert('Finished');")
objStringBuilder.Append("</script>")

This gives me an error message in the IDE:

statement cannot appear within a method body.  end of method assumed

The error is appearing on the last line:

objStringBuilder.Append("</script>")

This code is done in VB.NET, hence no semi-colons like C#.

5
  • Er, where are your semi-colons? :) Sep 28, 2012 at 8:50
  • No semi-colons in VB.NET, it's in the tag, and I have just updated the question to make this clear. Sep 28, 2012 at 8:51
  • When you say "Error in the IDE", do you mean a compile error? If so, aspnet101.com/2008/06/…
    – Tim M.
    Sep 28, 2012 at 8:58
  • I copy pasted the code you provide, and I cannot reproduce. It might come from somewhere else.
    – Larry
    Sep 28, 2012 at 9:02
  • Not a compile time error, a syntax error, i.e. when I finish typing the string builder, it gets a blue squiggly line underneath it. Sep 28, 2012 at 9:03

4 Answers 4

2

I'm going to take a (big) guess here.

  • assumption: you are working within script runat="server" blocks
  • assumption: closing the script tag (even as a string) closes the server block
  • server block is now malformed

Alternatively, it could be a malformed Sub or Function block.

0
1

Fixed it by changing objStringBuilder.Append("</script>") to objStringBuilder.Append("</scr" & "ipt>")...

0

As suggested from Asp.net forum:

to Insert Javascript from codebehind I prefer you to use

RegisterClientScript instead of plane string deployment to aspx page

you can use code like

 ScriptManage.RegisterClientScript(this.GetType(), "", "your javascript
    code", true);

or

> Page.ClientScript.RegisterClientScript("", "your javascript code",
> true);

you need to use your javascript code without tag name

http://forums.asp.net/t/1683634.aspx/1

0
StringBuilder Sb = new StringBuilder();
Sb.Append("alert('Finished');");
Page.ClientScript.RegisterStartupScript(this.GetType(), "", Sb.ToString(), true);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.