当前位置:首页 > ASP.NET常用JS脚本整理

ASP.NET常用JS脚本整理

点击次数:1764  更新日期:2010-12-28
\n

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


\n

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)
…{


\n

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);
}


\n


/**//// <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 );
}


\n

/**//// <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);
}


\n

/**//// <summary>
/// 弹出提示对话框
/// </summary>
/// <param name=”strMessage”>消息字符串</param>
public static void Alert(string strMessage)
…{
Alert(“alert”, strMessage);
}


\n


/**//// <summary>
/// 弹出提示对话框
/// </summary>
/// <param name=”name”>脚本块标识。</param>
/// <param name=”strMessage”>消息字符串</param>
public static void Alert(string name,string strMessage)
…{


\n

RegJs(name,string.Format(“<script>alert(‘{0}’);</script>”, strMessage));
}


\n

/**//// <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)
…{


\n

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


\n

/**//// <summary>
/// 弹出确认对话框
/// </summary>
/// <param name=”name”>脚本块标识.</param>
/// <param name=”strMessage”>消息字符串</param>
public static void Confirm(string name , string strMessage)
…{
RegJs(name,”<script> confirm(‘” + strMessage + “‘)</script>”);
}


\n


/**//// <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));
}


\n


/**//// <summary>
/// 关闭网页,生成关闭网页的脚本代码
/// </summary>
/// <returns>关闭网页的脚本代码</returns>
public static void ClosePage()
…{
StringBuilder js = new StringBuilder();
js.Append(“<script language=”JavaScript”>window.close();</script>”);
RegJs(“ClosePage”, js.ToString());
}


\n

/**//// <summary>
/// 生成调用浏览器打印的脚本代码
/// </summary>
/// <returns>调用浏览器打印的脚本代码</returns>
public static void PrintPage()
…{
StringBuilder js = new StringBuilder();
js.Append(“<script language=”JavaScript”>window.print();</script>”);
RegJs(“PrintPage”, js.ToString());
}


\n

/**//// <summary>
/// 生成打开窗口的脚本代码
/// </summary>
/// <param name=”url”>要打开的联接</param>
/// <returns>打开窗口的脚本代码</returns>
public static void OpenPage(string url)
…{
OpenPage(url, 700, 500);
}


\n


/**//// <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)
…{


\n

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());
}
}
}


\n

注:RegisterClientScriptBlock和RegisterStartupScript主要区别在于最终HTML代码中脚本出现的位置不同
用RegisterClientScriptBlock时一般出现在<form>标记后,而用和RegisterStartupScript时一般出现在</form>之前.所以如果脚本中如果涉及到与form交互则用RegisterStartupScript

来源:dreambroken的blog

\n