初心者Helper類のJSHelper

1707 ワード

public class JSHelper

    {

        /// <summary>

        ///  js 

        /// </summary>

        /// <param name="script">js </param>

        public static void RegisterScript(string script)

        {

            Page page = HttpContext.Current.Handler as Page;

            page.ClientScript.RegisterStartupScript(page.GetType(), "js2", string.Format("<script>{0}</script>", script));

        }



        /// <summary>

        /// JS 

        /// </summary>

        /// <param name="message"> </param>

        public static void Alert(string message)

        {

            Page page = HttpContext.Current.Handler as Page;

            Guid gid = new Guid();

            string jsblock = "js" + gid.ToString();

            page.ClientScript.RegisterStartupScript(page.GetType(), jsblock, string.Format("<script>alert(\"{0}\")</script>", message));

        }



        /// <summary>

        /// JS URL

        /// </summary>

        /// <param name="url"> URL</param>

        public static void GoToUrl(string url)

        {

            RegisterScript(string.Format("location.href='{0}'", url));

        }



        /// <summary>

        /// JS 

        /// </summary>

        public static void Refresh()

        {

            RegisterScript(string.Format ("location.reload();"));

        }



        public static void AlertAndRedirect(string message, string url)

        {

            Alert(message);

            GoToUrl(url);

        }

    }