My JScriptHelper class

I’ve posted a few code snippets and some people noticed that there are references to unresolved methods.I am using a few helper classes. This post describes my JScriptHelper class

 

      /// <summary>

      /// The JScriptHelper class should be a replacer of VB ClientScriptsHelper.

      /// However VB ClientScriptsHelper has calls to embedded resources, so it is not too easy to move

      /// TODO move VB methods to JScriptHelper in stages.

      /// </summary>

      public class JScriptHelper

      {

            public JScriptHelper()

            {

                  //

                  // TODO: Add constructor logic here

                  //

            }

            public static string JScript(string sScript)

            {

                  string[] textArray1 = new string[3] { “”, sScript, “” } ;

                  return string.Concat(textArray1);

            }

            public static string JSToString(bool bValue)

            {

                  return bValue.ToString().ToLower();

            }

            public static string NullIfFalse(bool bValue)

            {

                  if (bValue==false) return “null”;

                  return bValue.ToString().ToLower();

            }

            public static string QuotedClientId(Control ctrl)

            {

                  if (ctrl==null) return “null”;

                  return DataHelper.Quoted(ctrl.ClientID);

            }

            //created based on ResSrvHandler.cs 29/11/2005

            public static void RegisterStartupScriptUrl(System.Web.UI.Page page, string srcUrl)

            {

                  StringBuilder sb = new StringBuilder(/span);o:p/o:p/span/pp%20class=MsoNormal%20style=MARGIN:%200in%200in%200pt;%20mso-layout-grid-align:%20nonespan%20style=FONT-SIZE:%2010pt;%20FONT-FAMILY:);

            page.ClientScript.RegisterStartupScript(TypeForClientScript(), srcUrl, sb.ToString());

            }

            public static bool RegisterFunctionToPostBack(string sFunctionName,Control ctrl)

            { //from http://www.xefteri.com/articles/show.cfm?id=18 How postback works in ASP.NET

                  if (IsJavaScriptSupported())

                  {

                        //                            debugger;

                        //call the postback function with the right ID __doPostBack(‘” + UniqueIDWithDollars(ctrl) + @”‘,”);

                        string sJS =    function “ + sFunctionName + @”()

                        {

                        + ctrl.Page.ClientScript.GetPostBackEventReference(ctrl,“”) +@”

                        }”;

                        sJS=JScriptHelper.JScript(sJS);

                ctrl.Page.ClientScript.RegisterStartupScript(TypeForClientScript(), sFunctionName, sJS);

                        return true;

                  }

                  return false;

            }

        public static bool IsJavaScriptSupported()

        {

            return (HttpContext.Current.Request.Browser.EcmaScriptVersion.Major > 0);

        }

 

            // Call Page.GetPostBackEventReference instead (http://geekswithblogs.net/mnf/archive/2005/11/04/59081.aspx)

            //          //from http://www.codeproject.com/aspnet/DesignTimeSupport.asp

//          public static string UniqueIDWithDollars(Control ctrl)

//          {

//                      string sId = ctrl.UniqueID;

//                      if (sId == null)

//                      {

//                            return null;

//                      }

//                      if (sId.IndexOf(‘:’) >= 0)

//                      {

//                            return sId.Replace(‘:’, ‘$’);         

//                      }

//                      return sId;

//          }

 

 

            public static void SetRefresh(Page  page,int nDelaySec)

            { // from http://groups.google.com/group/microsoft.public.dotnet.framework.aspnet/msg/ffec2cc7c4836ed6?hl=en&

        // or http://www.eggheadcafe.com/forums/ForumPost.asp?ID=34068&INTID=2

 

                  //NOTE: Often the __doPostBack function is inserted into your page by .NET. If not, put the code in manually, or add a dummy LinkButton and set it’s style to be “display:none”.

                  //     from http://www.codeproject.com/aspnet/OneTimeClickableButton.asp?msg=1185901

                  //window.Form1.submit();

                  int nMilliSec=nDelaySec*1000;

                  StringBuilder sb = new StringBuilder();

                  sb.Append(“setTimeout(myRefresh,”+ nMilliSec.ToString()+ “);”);

                  sb.Append( @” function myRefresh()

                        {

                         __doPostBack(document.forms[0].id,”);

                        } “);

                  page.ClientScript.RegisterClientScriptBlock(TypeForClientScript(),“SetRefresh”, JScript(sb.ToString()));

            }

            public static void SetRefresh(System.Web.UI.HtmlControls.HtmlGenericControl mtaRefresh,int nDelay)

            { //DIsadvantage- not postback, but just get, possible workaround -add url with spetial flag, e.g &bRefreshed

                  //from http://www.informit.com/articles/article.asp?p=174363&rl=1

                  //                ‘ use META REFRESH to start loading next page

                  mtaRefresh.Attributes.Add(“http-equiv”, “refresh”);

                  mtaRefresh.Attributes.Add(“content”,nDelay.ToString());//”0;url=” & sRefreshURL)

 

            }

        private static Type  TypeForClientScript()

        {

            return typeof(JScriptHelper);

        }

 

      }

 

 

 

 

 

Advertisements