kamagra how much to take

Use A Builder With JavaScript In WebParts

It is pretty easy to use JavaScript in your WebParts, and people that know me, know I frickin despise JavaScript. I really, really do. I know sometimes I have to use it, but I would prefer if it didn’t exist, even though I might start warming up to our old friend as Orcas since it is supposed to have better debugging and intellisense support (finally). Outside of my personal disdain for it, sometimes I have to use JavaScript (thanks to Trenton Kuzman who wrote my last script for my last WebPart at CENTAF:-) ), and I usually build it out by using an ArrayList object and passing it into a builder method so that I hydrate the builder object in my primary WebPart class file (which traditionally for this layer of abstraction would be kept in a custom WebPart base class that instead inherits from the WebPart base class, assimilated into your WebPart class file subsequently).

So basically, I want to be able to do this in an arbitrary WebPart class file:

  1. private void BuildExampleJavaScript (TextWriter oWriter)
  2. {
  3. oWriter.Write("<a href="javascript:buenz_ExampleFunction('{0}');">Do Something</a>", ID);
  4.  
  5. // This is my function below, now I want to add some statements
  6. JavaScriptBuilder l_oBuilder = new JavaScriptBuilder();
  7. l_oBuilder.AddStatement("function buenz_ExampleFunction(id){");
  8. l_oBuilder.AddStatement("try{'arbitrary Statements}");
  9. oWriter.Write(l_oBuilder.ScriptString);
  10. }

It is cleaner to me. So I use this class file:

  1. using System;
  2. using System.Collections;
  3. using System.Text;
  4.  
  5. namespace buenz.Examples
  6. {
  7. public class JavaScriptBuilder : IJavaScriptBuilder
  8. {
  9. private readonly ArrayList m_oStatements = new ArrayList();
  10.  
  11. public void AddStatement(string line)
  12. {
  13. m_oStatements.Add(line);
  14. }
  15.  
  16. public string ScriptString
  17. {
  18. get
  19. {
  20. StringBuilder l_oBuilder = new StringBuilder(100);
  21. l_oBuilder.Append("<script language="JavaScript">");
  22. l_oBuilder.Append(Environment.NewLine);
  23. l_oBuilder.Append(Environment.NewLine);
  24. foreach (string text in m_oStatements)
  25. {
  26. l_oBuilder.Append(text);
  27. l_oBuilder.Append(Environment.NewLine);
  28. }
  29. l_oBuilder.Append(Environment.NewLine);
  30. l_oBuilder.Append("</script>");
  31. return l_oBuilder.ToString();
  32. }
  33. }
  34. }
  35. }

and then this interface (to adhere to Design by Contract programming principles):

  1. namespace buenz.Example
  2. {
  3. public interface IJavaScriptBuilder
  4. {
  5. string ScriptString { get; }
  6.  
  7. void AddStatement(string strLine);
  8. }
  9. }

which let’s me do what I want to accomplish. It looks cleaner and provides finer seperation IMHO.

Share

One Comment

  1. bazztrap says:

    I like it, but I would rather prefer javascript files rather embedded in C# code. If you add file in Class resource directory .. then you have an option of haveing different JS files for different virtual servers and Also any updates or adding additional features on Javascript end wont lead to editing your C# code rather just changes in JS files

    So in that way you have same webpart in 2 different sites but using different javascript behavior

Trackbacks/Pingbacks

  1. SharePoint 2007 Link love: 10-14-2007 at Virtual Generations - [...]  Cannot complete this action.Use A Builder With JavaScript In WebParts(Adam Buenz, SharePoint Shelter) [...]

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>