给大家分享一个通用PAGE类
2008-08-06 16:17
411 查看
c# 代码
/********************************************************
* 功能名称:页面基类(前台页面应用)
* 功能描述:
* 创建者:水瓶
* 创建日期:2007/7/14
* 修改者:
* 修改日期:
* *******************************************************/
using System;
using System.Web;
using System.Web.UI;
using System.Web.SessionState;
using System.Diagnostics;
using System.Text.RegularExpressions;
namespace ITNB.Base
{
/// <summary>
/// 页面基类,应用于不涉及SESSION的页面运行
/// </summary>
public class PageCommon : System.Web.UI.Page
{
public PageCommon()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region Javascript 脚本
/// <summary>
/// 弹出JavaScript信息小窗口并跳转页面。
/// </summary>
/// <param name="message">弹出消息</param>
/// <param name="toURL">跳转页面链接</param>
protected void JsAlertAndRedirect(string message, string toURL)
{
string js = "<script language=javascript>alert({0});window.location.replace({1})</script>";
this.Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), String.Format(js, message, toURL));
}
/// <summary>
/// 跳转页面。
/// </summary>
/// <param name="toURL">跳转页面链接</param>
protected void JsRedirect(string toURL)
{
string js = "<script language=javascript>window.location.replace({0})</script>";
this.Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), String.Format(js, toURL));
}
/// <summary>
/// 弹出JavaScript信息小窗口并关闭窗口。
/// </summary>
/// <param name="message"></param>
protected void JsAlertAndClose(string message)
{
string js = "<script language=javascript>alert({0});this.close();</script>";
this.Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), String.Format(js, message));
}
/// <summary>
/// 弹出JavaScript信息小窗口并跳转页面。
/// </summary>
/// <param name="message">弹出消息</param>
/// <param name="toURL">跳转页面链接</param>
protected void JsAlertAndRedirect(object message, string toURL)
{
string js = "<script language=javascript>alert({0});window.location.replace({1})</script>";
this.Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), String.Format(js, message, toURL));
}
/// <summary>
/// 弹出JavaScript信息小窗口
/// </summary>
/// <param name="message">窗口信息</param>
protected void JsAlert(string message)
{
string js = @"<Script language=JavaScript>alert({0});</Script>";
this.Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), String.Format(js, message));
}
/// <summary>
/// 弹出JavaScript信息小窗口
/// </summary>
/// <param name="message">窗口信息</param>
protected void JsAlert(object message)
{
string js = @"<Script language=JavaScript>alert({0});</Script>";
this.Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), String.Format(js, message));
}
/// <summary>
/// 弹出Javascript确认消息框
/// </summary>
/// <param name="message">窗口信息</param>
protected void JsConfirmMessage(object message)
{
string js = @"<Script language=JavaScript>strWinCtrl = true;
strWinCtrl = if(!confirm({0}))return false;</Script>";
this.Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), String.Format(js, message));
}
/// <summary>
/// 回到历史页面
/// </summary>
/// <param name="value">-1/1</param>
protected void JsGoHistory(int value)
{
string js = @"<Script language=JavaScript>history.go({0});</Script>";
this.Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), String.Format(js, value));
}
/// <summary>
/// 关闭当前窗口
/// </summary>
protected void JsCloseWindow()
{
string js = @"<Script language=JavaScript>window.close();</Script>";
this.Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), js);
this.Page.Response.End();
}
/// <summary>
/// 刷新父窗口
/// </summary>
protected void JsRefreshParent()
{
string js = @"<Script language=JavaScript>parent.location.reload();</Script>";
this.Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), js);
}
protected void JsAlertCloseAndRefreshParent(string message)
{
string js = @"<script>alert(" + message + ");window.close();window.opener.location.reload();</script>";
this.Page.ClientScript.RegisterStartupScript(Page.GetType(), Guid.NewGuid().ToString(), js);
}
#endregion
#region 数据的加密码与解密
/// <summary>
/// 加密数据
/// </summary>
/// <param name="input">要加密的数据</param>
/// <returns></returns>
protected string DataEncrypt(string input)
{
return ITNB.Base.Encrypted.Encrypt(input);
}
/// <summary>
/// 解密数据
/// </summary>
/// <param name="input">要解密的数据</param>
/// <returns></returns>
protected string DataDecrypt(string input)
{
return ITNB.Base.Encrypted.Decrypt(input);
}
#endregion
#region DataTable直接绑定页面的控件
/// <summary>
/// 将一个DataTable直接绑定到页面。注:DataTable中只有一行记录。
/// 此方法会自动将数据绑定到页的TextBox,Label,CheckBox等。
/// </summary>
/// <param name="dt">数据表</param>
/// <param name="container">页面容器</param>
protected void SetControlsValue(System.Data.DataTable dt, Control container)
{
ITNB.Base.ControlValue.SetControlsValue(dt, container);
}
#endregion
#region 保存数据后将页面控件置空
/// <summary>
/// 保存数据后将页面控件置空
/// </summary>
/// <param name="container">页面的容器</param>
protected void SetControlsValueEmpty(Control container)
{
ITNB.Base.ControlValue.SetControlsValueEmpty(container);
}
#endregion
#region 获取REQUEST的值
/// <summary>
/// 获取QueryString的值
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
protected string GetQueryString(string key)
{
object obj = System.Web.HttpContext.Current.Request.QueryString[key];
return obj != null ? obj.ToString() : string.Empty;
}
/// <summary>
/// 获取FormString的值
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
protected string GetFormString(string key)
{
object obj = System.Web.HttpContext.Current.Request.Form[key];
return obj != null ? obj.ToString() : string.Empty;
}
#endregion
#region 获取安全的字符串
/// <summary>
/// 获取安全的SQL字符串
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
protected string GetSafeSQLString(string sql)
{
sql = sql.Replace(",", ",");
sql = sql.Replace(".", "。");
sql = sql.Replace("(", "(");
sql = sql.Replace(")", ")");
sql = sql.Replace(">", ">");
sql = sql.Replace("<", "<");
sql = sql.Replace("-", "-");
sql = sql.Replace("+", "+");
sql = sql.Replace("=", "=");
sql = sql.Replace("?", "?");
sql = sql.Replace("*", "*");
sql = sql.Replace("|", "|");
sql = sql.Replace("&", "&");
return sql;
}
/// <summary>
/// 判断给定的字符串(strNumber)是否是数值型
/// </summary>
/// <param name="strNumber">要确认的字符串</param>
/// <returns>是则返加true 不是则返回 false</returns>
public static bool IsNumber(string strNumber)
{
return new Regex(@"^[0-9]$").IsMatch(strNumber);
}
public int CutIntFromStr(string str)
{
string sr = "";
for (int i = 0; i < str.Length; i++)
{
if (IsNumber(str[i].ToString()))
{
sr += str[i].ToString();
}
}
return Convert.ToInt32(sr);
}
#endregion
}
}
/********************************************************
* 功能名称:页面基类(前台页面应用)
* 功能描述:
* 创建者:水瓶
* 创建日期:2007/7/14
* 修改者:
* 修改日期:
* *******************************************************/
using System;
using System.Web;
using System.Web.UI;
using System.Web.SessionState;
using System.Diagnostics;
using System.Text.RegularExpressions;
namespace ITNB.Base
{
/// <summary>
/// 页面基类,应用于不涉及SESSION的页面运行
/// </summary>
public class PageCommon : System.Web.UI.Page
{
public PageCommon()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region Javascript 脚本
/// <summary>
/// 弹出JavaScript信息小窗口并跳转页面。
/// </summary>
/// <param name="message">弹出消息</param>
/// <param name="toURL">跳转页面链接</param>
protected void JsAlertAndRedirect(string message, string toURL)
{
string js = "<script language=javascript>alert({0});window.location.replace({1})</script>";
this.Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), String.Format(js, message, toURL));
}
/// <summary>
/// 跳转页面。
/// </summary>
/// <param name="toURL">跳转页面链接</param>
protected void JsRedirect(string toURL)
{
string js = "<script language=javascript>window.location.replace({0})</script>";
this.Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), String.Format(js, toURL));
}
/// <summary>
/// 弹出JavaScript信息小窗口并关闭窗口。
/// </summary>
/// <param name="message"></param>
protected void JsAlertAndClose(string message)
{
string js = "<script language=javascript>alert({0});this.close();</script>";
this.Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), String.Format(js, message));
}
/// <summary>
/// 弹出JavaScript信息小窗口并跳转页面。
/// </summary>
/// <param name="message">弹出消息</param>
/// <param name="toURL">跳转页面链接</param>
protected void JsAlertAndRedirect(object message, string toURL)
{
string js = "<script language=javascript>alert({0});window.location.replace({1})</script>";
this.Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), String.Format(js, message, toURL));
}
/// <summary>
/// 弹出JavaScript信息小窗口
/// </summary>
/// <param name="message">窗口信息</param>
protected void JsAlert(string message)
{
string js = @"<Script language=JavaScript>alert({0});</Script>";
this.Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), String.Format(js, message));
}
/// <summary>
/// 弹出JavaScript信息小窗口
/// </summary>
/// <param name="message">窗口信息</param>
protected void JsAlert(object message)
{
string js = @"<Script language=JavaScript>alert({0});</Script>";
this.Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), String.Format(js, message));
}
/// <summary>
/// 弹出Javascript确认消息框
/// </summary>
/// <param name="message">窗口信息</param>
protected void JsConfirmMessage(object message)
{
string js = @"<Script language=JavaScript>strWinCtrl = true;
strWinCtrl = if(!confirm({0}))return false;</Script>";
this.Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), String.Format(js, message));
}
/// <summary>
/// 回到历史页面
/// </summary>
/// <param name="value">-1/1</param>
protected void JsGoHistory(int value)
{
string js = @"<Script language=JavaScript>history.go({0});</Script>";
this.Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), String.Format(js, value));
}
/// <summary>
/// 关闭当前窗口
/// </summary>
protected void JsCloseWindow()
{
string js = @"<Script language=JavaScript>window.close();</Script>";
this.Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), js);
this.Page.Response.End();
}
/// <summary>
/// 刷新父窗口
/// </summary>
protected void JsRefreshParent()
{
string js = @"<Script language=JavaScript>parent.location.reload();</Script>";
this.Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), js);
}
protected void JsAlertCloseAndRefreshParent(string message)
{
string js = @"<script>alert(" + message + ");window.close();window.opener.location.reload();</script>";
this.Page.ClientScript.RegisterStartupScript(Page.GetType(), Guid.NewGuid().ToString(), js);
}
#endregion
#region 数据的加密码与解密
/// <summary>
/// 加密数据
/// </summary>
/// <param name="input">要加密的数据</param>
/// <returns></returns>
protected string DataEncrypt(string input)
{
return ITNB.Base.Encrypted.Encrypt(input);
}
/// <summary>
/// 解密数据
/// </summary>
/// <param name="input">要解密的数据</param>
/// <returns></returns>
protected string DataDecrypt(string input)
{
return ITNB.Base.Encrypted.Decrypt(input);
}
#endregion
#region DataTable直接绑定页面的控件
/// <summary>
/// 将一个DataTable直接绑定到页面。注:DataTable中只有一行记录。
/// 此方法会自动将数据绑定到页的TextBox,Label,CheckBox等。
/// </summary>
/// <param name="dt">数据表</param>
/// <param name="container">页面容器</param>
protected void SetControlsValue(System.Data.DataTable dt, Control container)
{
ITNB.Base.ControlValue.SetControlsValue(dt, container);
}
#endregion
#region 保存数据后将页面控件置空
/// <summary>
/// 保存数据后将页面控件置空
/// </summary>
/// <param name="container">页面的容器</param>
protected void SetControlsValueEmpty(Control container)
{
ITNB.Base.ControlValue.SetControlsValueEmpty(container);
}
#endregion
#region 获取REQUEST的值
/// <summary>
/// 获取QueryString的值
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
protected string GetQueryString(string key)
{
object obj = System.Web.HttpContext.Current.Request.QueryString[key];
return obj != null ? obj.ToString() : string.Empty;
}
/// <summary>
/// 获取FormString的值
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
protected string GetFormString(string key)
{
object obj = System.Web.HttpContext.Current.Request.Form[key];
return obj != null ? obj.ToString() : string.Empty;
}
#endregion
#region 获取安全的字符串
/// <summary>
/// 获取安全的SQL字符串
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
protected string GetSafeSQLString(string sql)
{
sql = sql.Replace(",", ",");
sql = sql.Replace(".", "。");
sql = sql.Replace("(", "(");
sql = sql.Replace(")", ")");
sql = sql.Replace(">", ">");
sql = sql.Replace("<", "<");
sql = sql.Replace("-", "-");
sql = sql.Replace("+", "+");
sql = sql.Replace("=", "=");
sql = sql.Replace("?", "?");
sql = sql.Replace("*", "*");
sql = sql.Replace("|", "|");
sql = sql.Replace("&", "&");
return sql;
}
/// <summary>
/// 判断给定的字符串(strNumber)是否是数值型
/// </summary>
/// <param name="strNumber">要确认的字符串</param>
/// <returns>是则返加true 不是则返回 false</returns>
public static bool IsNumber(string strNumber)
{
return new Regex(@"^[0-9]$").IsMatch(strNumber);
}
public int CutIntFromStr(string str)
{
string sr = "";
for (int i = 0; i < str.Length; i++)
{
if (IsNumber(str[i].ToString()))
{
sr += str[i].ToString();
}
}
return Convert.ToInt32(sr);
}
#endregion
}
}
相关文章推荐
- 和大家分享一个Makefile的通用写法
- 跟大家分享一个非常精简的夏令时转换程序,程序是通用的可以在51、430、PIC、瑞萨系列的单片机上使用
- 和大家分享一个Makefile的通用写法
- C# PDF Page操作——设置页面切换按钮 C# 添加、读取Word脚注尾注 C#为什么不能像C/C++一样的支持函数只读传参 web 给大家分享一个好玩的东西,也许你那块就用的到
- 太感动了,转载他人让大家也分享 一个妓女死了,学校为她降半旗!(无比崇敬)---希望朋友能转载一下!
- 为大家分享一个 Ajax Loading —— spin.js
- 给大家分享一个自定义Winform控件类
- 用ajax.dll来做一个异步数据的交互例子大家分享
- 和大家分享一个小TIP
- 最近写了一个HTTP代理服务器, 有些心得, 与大家分享
- 和大家分享下,自己写的一个截图小工具
- 给大家分享一个图片双击放大,双击缩小的第三方库
- 自己写了一个带placeHolder的textView,分享给大家
- 一个通用的Confirmation Page类
- 今天为大家分享各个浏览器通用的QQ在线客服代码
- 分享一个MVC的多层架构,欢迎大家拍砖斧正
- “一个非常漂亮的炫耀窗口的源代码”- 与大家分享
- 在看官方的例子时候由一个grid实例,在每一行之前有个加号,展开可以显示内容,在实际应用中还是非常有用的,照搬例子的代码,发现老是提示对象不存在,查阅资料后发现是没有加载插件,这里和大家分享下Ext.grid.RowExpander插件的使用,
- 偶然看到一个GridView的妙用,感觉很好,转载过来和大家分享下,很感谢博主