天天看点

ASP.NET常用JS脚本整理

Asp.net设计时经常要在.aspx.cs代码中写一些脚本,每次都重新写比较麻烦,所以把经常用的整理了一下,写成一个公共类,以便以后调用.

using System.Text;

using System.Web;

using System.Web.UI; namespace Lmsoft.Net.Web

{

    /**//// <summary>

    /// 文件名: Js.cs

    /// 功能描述: .NET程序中常用的Javascipt脚本

    /// </summary>

    public class Js

    {

        /**//// <summary>

        /// 在客户端注册Javascript脚本,以便调用

        /// </summary>

        /// <param name="name">脚本块标识。</param>

        /// <param name="content">脚本代码</param>

        public static void RegJs(string name, string content)

        {             string js = content;

            if (js.IndexOf("<script") < 0)

            {

                js = "<script language='JavaScript'>" + js + "</script>";

            }

            Page page = (Page)HttpContext.Current.Handler;

            page.ClientScript.RegisterClientScriptBlock(page.GetType(), name, js);

        }

        /// 在客户端执行一段脚本

        /// <param name="js">要执行的脚本</param>

        public static void ExeJs(string name,string js)

        {

                js = string.Format("<script language='JavaScript'>{0}</script>", js);

            page.ClientScript.RegisterStartupScript(page.GetType(),name, js );

        }         /**//// <summary>

        /// 回车转Tab键

        public static void EnterToTab()

              string EnterToTab = "<script language='javascript' event='onkeydown' for='document'> if(event.keyCode==13 && event.srcElement.type!='button' && event.srcElement.type!='submit' && event.srcElement.type!='reset' && event.srcElement.type!='textarea' && event.srcElement.type!='' && event.srcElement.type!='imagebutton')event.keyCode=9;</script>";

            RegJs("enterToTab",EnterToTab);

        /// 弹出提示对话框

        /// <param name="strMessage">消息字符串</param>

        public static void Alert(string strMessage)

            Alert("alert", strMessage);

        public static void Alert(string name,string strMessage)

        {                     RegJs(name,string.Format("<script>alert('{0}');</script>", strMessage));

        /// 服务器端弹出alert对话框,并使控件获得焦点

        /// <param name="name">脚本块标识。-

</param>

        /// <param name="str_Message">提示信息</param>

        /// <param name="focusControl">需要获得焦点的控件名称</param>

        public static void Alert(string name, string str_Message,string focusControl)

        {             ExeJs(name, string.Format("<script>alert('{0}');document.forms(0).{1}.focus(); document.forms(0).{2}.select();</script>", str_Message, focusControl, focusControl));

        /// 弹出确认对话框

        /// <param name="name">脚本块标识.</param>

        public static void Confirm(string name , string strMessage)

            RegJs(name,"<script> confirm('" + strMessage + "')</script>");

        /// 使控件获得焦点

        /// <param name="ctlId">获得焦点控件Id值,比如:txt_Name</param>

        public static void GetFocus(string ctlId)

            ExeJs("GetFocus", string.Format("<script>document.getElementById('{0}').focus(); document.getElementById('{1}').select();</script>", ctlId, ctlId));

        /// 关闭网页,生成关闭网页的脚本代码

        /// <returns>关闭网页的脚本代码</returns>

        public static void ClosePage()

            StringBuilder js = new StringBuilder();

            js.Append("<script language='JavaScript'>window.close();</script>");

            RegJs("ClosePage", js.ToString());

        /// 生成调用浏览器打印的脚本代码

        /// <returns>调用浏览器打印的脚本代码</returns>

        public static void PrintPage()

            js.Append("<script language='JavaScript'>window.print();</script>");

            RegJs("PrintPage", js.ToString());

        /// 生成打开窗口的脚本代码

        /// <param name="url">要打开的联接</param>

        /// <returns>打开窗口的脚本代码</returns>

        public static void  OpenPage(string url)

            OpenPage(url, 700, 500);

        /// <param name="width">窗口宽度</param>

        /// <param name="height">窗口高度</param>

        public static void OpenPage(string url, int width, int height)

        {             StringBuilder js = new StringBuilder();

            js.Append("<script language='JavaScript'>");

            js.Append("window.open('");

            js.Append(url);

;     js.AppendFormat("', '_blank', 'left=20,top=20,height={0},width={1},toolbar=no, menubar=no,scrollbars=yes, resizable=yes, location=no,status=no')", height, width);

            js.Append("</script>");

            RegJs("OpenPage",js.ToString());

        }

    }

}

继续阅读