Link to home
Create AccountLog in
Avatar of Samuraiken

asked on

How to use Javascript within C# (Used in writting Web Parts for WSS 3.0)

I have the following code, and numerous people have led me to believe that i can use javascript within c#  when i'm creating a web part for windows sharepoint services 3.0.  However, even just using  a basic test class to see if it would work, i met with failure.  

Can someone look at this and see what might be wrong?  Or fix it and show me what i did wrong?  Do i need to add a special reference for it to work?

I know the javascript works, as i did it myself on my desk as well as tested it with W3Schools "Try It" program (type in javascript online and it outputs it for you), however it just isn't working for me inside of a web part when i add it to my page.

using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using System.Timers;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
namespace readList
    public class Hello : Microsoft.SharePoint.WebPartPages.WebPart
        protected override void RenderContents(HtmlTextWriter writer)
            string JavascriptCommands;
            JavascriptCommands = @"<html>";
            JavascriptCommands += @"<body>";
            JavascriptCommands += @"<script type='text/javascript'>";
            JavascriptCommands += @"document.write('I'm a string, yo!');";
            JavascriptCommands += @"</script>";
            JavascriptCommands += @"</body>";
            JavascriptCommands += @"</html>";

Open in new window

Avatar of ZachSmith
Flag of United States of America image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of hielo
Flag of Wallis and Futuna image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
The solution suggested by hielo is what I implemented in my code block. Hopefully it works!
Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
jandromeda makes a good point :)
Avatar of Samuraiken


I understand what immutability is (i actually had never heard of it and just looked it up, but i understand the concept).  However, it's the same variable right?  I mean, each time i do += "foo" it's asking for more memory spaces but at the same time...isn't that what "+=" was built for?

As far as not escaping correctly, i saw that after i came back from lunch.  Good point and thanks for catching it!

This worked:
writer.Write("document.write('Im a string, yo!');");

And i'm still a little confused on how to actually use StringBuilder, but i wanted to ask if you could explain something else...

So if i was using the same variable (in the above code) and just concatenating it with +=, by the time it got to the end of the code, JavascriptCommands would = "</html>"; ?  Or are the previous values in the string still out there in memory somewhere?  Even if the string object was destroyed and created again i would think that it would keep the values intact (otherwise i dont see why we even have concatenation via += at all)?

Just a little confused, but i a million thanks for helping, everyone.  It's hard enough to "learn on the fly" with WSS and C#, making it worse because i couldn't get a simple JS script to run was making me lose more hair than i'd like to admit!
Thank you very much!
Zach and Heilo, i had wondered about escaping that extra ' so i just removed it for simplicity.
Jandromeda, i had never thought of that and...what a shock.  Thank you very much!
If you could take a look at the last post i made i would be grateful, but if not it's fine.  I'll just use writer.write to write the javascript for now on.

One other thing i would like to ask, or maybe i should just make another question, is this....
I got a javascript clock online from someone's page.  I made a new .html file on my desktop and the script ran fine.  However, when i put that script into visual studio, and wrap each javascript line with:

writer.write(" <javascript here>");

It doesn't build the clock.  I've replaced all double-quotes with single-quotes in the javascript so that it wont interfere with anything on c#'s side.  

However, do html form events kick off like normal, or not?

writer.Write("<body onload='StartClock()' onunload='KillClock()'>");

The clock isn't starting, and i'm assuming this is why...the page itself is loaded already so it problably isn't picking up on "body onload()".

Any ideas?
Replace your code with this.

StringBuilder builder = new StringBuilder();
            builder.AppendLine(@"<script type='text/javascript'>");
            builder.AppendLine(@"document.write('I\'m a string, yo!');");

And refer this article to learn about strings in .Net. Bit lengthy but worth reading.