using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
/// <summary>
/// ComFunction 的摘要说明
/// 一些通用的方法
/// </summary>
public class ComFunction
{
public ComFunction()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region "返回用户登录名"
/// <summary>
/// 返回用户登录名
/// </summary>
/// <returns></returns>
public static string GetUserEnName()
{
string strUserEnName = HttpContext.Current.User.Identity.Name;
strUserEnName = strUserEnName.Substring(strUserEnName.IndexOf('\\') + 1);
return strUserEnName;
}
#endregion
#region "设置不缓存网页"
/// <summary>
/// 设置不缓存网页
/// </summary>
public static void SetNoWebCache()
{
HttpContext.Current.Response.Cache.SetNoServerCaching();
HttpContext.Current.Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
HttpContext.Current.Response.Cache.SetNoStore();
HttpContext.Current.Response.Cache.SetExpires(new DateTime(1900, 01, 01, 00, 00, 00, 00));
}
#endregion
#region "在当前页面弹出提示框"
/// <summary>
/// 在当前页面弹出提示框
/// 傅威2006-06-08
/// </summary>
/// <param name="page"></param>
/// <param name="o"></param>
/// <param name="strText"></param>
public static void ShowMessageBox(Page page,string strText)
{
strText = strText.Replace("'", "").Replace(";", "").Replace("<", "").Replace(">", "").Replace("\\","/").Trim();
page.ClientScript.RegisterStartupScript(page.GetType(), "", "<script>alert('" + strText + "');</script>");
}
#endregion
#region "在当前页面弹出错误提示信息"
/// <summary>
/// 在当前页面弹出提示框
/// 傅威2006-06-08
/// </summary>
/// <param name="page"></param>
/// <param name="o"></param>
/// <param name="strText"></param>
public static void ShowErrorMessage(Page page, string strError)
{
strError = strError.Replace("'", "").Replace(";", "").Replace("<", "").Replace(">", "").Replace("\\", "/").Trim();
page.ClientScript.RegisterStartupScript(page.GetType(), "", "<script>alert('" + "操作失败!错误原因:" + ComFunction.CutString(strError) + "');</script>");
}
#endregion
#region "将ASP.NET 2.0 TextBox设置为只读"
/// <summary>
/// 将TextBox设置为只读
/// 2006-05-22
/// Written By FuWei
/// </summary>
/// <param name="textBox"></param>
public static void SetTextBoxReadOnly(TextBox textBox)
{
textBox.Attributes["readonly"] = "true";
}
#endregion
#region "读取页面参数"
/// <summary>
/// 返回页面传送参数
/// 傅威编写
/// 2006-08-08
/// </summary>
///<param name="strPara">获取值页面</param>
/// <param name="strPara">参数名称</param>
/// <returns>返回值</returns>
public static string GetParameter(Page page, string strParaName)
{
string strVal = "";
if (page.Request.QueryString[strParaName] != null)
{
strVal = page.Request.QueryString[strParaName];
}
return strVal;
}
#endregion
#region "检查文件大小"
public static bool CheckFileLen(FileUpload fileUpLoad)
{
if (fileUpLoad.PostedFile.ContentLength / 1024.0 > WebConfigSet.GetMaxFileLen())
{
return false;
}
else
return true;
}
#endregion
#region "控件或得焦点"
public static void setFocus(Control controls, Page page)
{
string strScript = String.Format("<script language=JavaScript>document.getElementById('{0}').focus();</script>", controls.ID);
page.ClientScript.RegisterStartupScript(page.GetType(), "setFocus", strScript);
}
#endregion
#region 剪切字符串
/// <summary>
/// 剪切字符传
/// </summary>
/// <param name="s">代剪切字符串</param>
/// <returns>剪切后的字符串</returns>
public static string CutString(string s)
{
if (s.Length < 20)
{
return s;
}
else
{
s = s.Substring(0, 20) + "...";
return s;
}
}
#endregion
}