matthew Zhang

慢慢学习,慢慢进步!
  博客园  :: 首页  :: 新随笔  :: 订阅 订阅  :: 管理

通用Page类

Posted on 2008-10-31 09:32  matthewZhang  阅读(294)  评论(0编辑  收藏  举报
/********************************************************   
* 功能名称:页面基类(前台页面应用)
* 功能描述:
* 创建者:水瓶
* 创建日期: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
}
}