博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

ASP.NET常用JS脚本整理

Posted on 2009-05-21 08:48  linFen  阅读(893)  评论(0编辑  收藏  举报

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);
        }
        /**//// <summary>
        /// 在客户端执行一段脚本
        /// </summary>
        /// <param name="name">脚本块标识。</param>
        /// <param name="js">要执行的脚本</param>
        public static void ExeJs(string name,string js)
        {
            if (js.IndexOf("<script") < 0)
            {
                js = string.Format("<script language='JavaScript'>{0}</script>", js);
            }
            Page page = (Page)HttpContext.Current.Handler;
            page.ClientScript.RegisterStartupScript(page.GetType(),name, js );
        }         /**//// <summary>
        /// 回车转Tab键
        /// </summary>
        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);
        }         /**//// <summary>
        /// 弹出提示对话框
        /// </summary>
        /// <param name="strMessage">消息字符串</param>
        public static void Alert(string strMessage)
        {
            Alert("alert", strMessage);
        }
        /**//// <summary>
        /// 弹出提示对话框
        /// </summary>
        /// <param name="name">脚本块标识。</param>
        /// <param name="strMessage">消息字符串</param>
        public static void Alert(string name,string strMessage)
        {                     RegJs(name,string.Format("<script>alert('{0}');</script>", strMessage));
        }         /**//// <summary>
        /// 服务器端弹出alert对话框,并使控件获得焦点
        /// </summary>
        /// <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));
        }         /**//// <summary>
        /// 弹出确认对话框
        /// </summary>
        /// <param name="name">脚本块标识.</param>
        /// <param name="strMessage">消息字符串</param>
        public static void Confirm(string name , string strMessage)
        {
            RegJs(name,"<script> confirm('" + strMessage + "')</script>");
        }
        /**//// <summary>
        /// 使控件获得焦点
        /// </summary>
        /// <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));
        }
        /**//// <summary>
        /// 关闭网页,生成关闭网页的脚本代码
        /// </summary>
        /// <returns>关闭网页的脚本代码</returns>
        public static void ClosePage()
        {
            StringBuilder js = new StringBuilder();
            js.Append("<script language='JavaScript'>window.close();</script>");
            RegJs("ClosePage", js.ToString());
        }         /**//// <summary>
        /// 生成调用浏览器打印的脚本代码
        /// </summary>
        /// <returns>调用浏览器打印的脚本代码</returns>
        public static void PrintPage()
        {
            StringBuilder js = new StringBuilder();
            js.Append("<script language='JavaScript'>window.print();</script>");
            RegJs("PrintPage", js.ToString());
        }         /**//// <summary>
        /// 生成打开窗口的脚本代码
        /// </summary>
        /// <param name="url">要打开的联接</param>
        /// <returns>打开窗口的脚本代码</returns>
        public static void  OpenPage(string url)
        {
            OpenPage(url, 700, 500);
        }
        /**//// <summary>
        /// 生成打开窗口的脚本代码
        /// </summary>
        /// <param name="url">要打开的联接</param>
        /// <param name="width">窗口宽度</param>
        /// <param name="height">窗口高度</param>
        /// <returns>打开窗口的脚本代码</returns>
        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());
        }
    }
}
注:RegisterClientScriptBlock和RegisterStartupScript主要区别在于最终HTML代码中脚本出现的位置不同
用RegisterClientScriptBlock时一般出现在<form>标记后,而用和RegisterStartupScript时一般出现在</form>之前.所以如果脚本中如果涉及到与form交互则用RegisterStartupScript


文章出处:http://www.diybl.com/course/4_webprogram/asp.net/asp_netshl/2008612/124971_3.html