using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.Text;
/// <summary>
/// WebUI 的摘要说明
/// </summary>
public class WebUI
{
public WebUI()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region MD5加密
/// <summary>
/// MD5加密
/// </summary>
/// <param name="pwd">需要加密的字符串</param>
/// <param name="move">偏移量</param>
/// <returns>加密后字符串</returns>
public static string GetMD5Str(string pwd, string move)
{
System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] bytValue, bytHash;
bytValue = System.Text.Encoding.UTF8.GetBytes(move + pwd);
bytHash = md5.ComputeHash(bytValue);
md5.Clear();
string pwdMD5Str = "";
for (int i = 0; i < bytHash.Length; i++)
{
pwdMD5Str += bytHash[i].ToString("x").PadLeft(2, '0');
}
return pwdMD5Str;
}
//WebUI.GetMD5Str(pwd, "")
#endregion
#region Cookie登陆验证
public static void isLogin()
{
if (HttpContext.Current.Request.Cookies["myCookie"] == null)
{
HttpContext.Current.Response.Redirect("~/Login.aspx");
}
}
#endregion
#region Alert()提示
/// <summary>
/// 弹出JavaScript小窗口
/// </summary>
/// <param name="js">窗口信息</param>
public static void Alert(string message, Page page)
{
string js = @"<Script language='JavaScript'>
alert('" + message + "');</Script>";
if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "alert"))
{
page.ClientScript.RegisterStartupScript(page.GetType(), "alert", js);
}
}
/// <summary>
/// 弹出消息框并且转向到新的URL
/// </summary>
/// <param name="message">消息内容</param>
/// <param name="toURL">连接地址</param>
public static void AlertAndRedirect(string message, string toURL, Page page)
{
string js = "<script language=javascript>alert('{0}');window.location.href('{1}')</script>";
if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "AlertAndRedirect"))
{
page.ClientScript.RegisterStartupScript(page.GetType(), "AlertAndRedirect", string.Format(js, message, toURL));
}
}
#endregion
#region 上传图片
/// <summary>
/// 上传图片
/// </summary>
/// <param name="fileUpName">FileUpload控件名</param>
/// <param name="imageUrl">上传文件夹路径</param>
/// <param name="dataUrl">插入数据库的路径值</param>
/// <returns></returns>
public static string UpLoadImage(System.Web.UI.WebControls.FileUpload fileUpName, string imageUrl)
{
string dataUrl = "";
if (fileUpName.HasFile)
{
string fullfilename = fileUpName.PostedFile.FileName;//上传文件 本地 路径
string type = fullfilename.Substring(fullfilename.LastIndexOf(".") + 1).ToLower(); //得到加载文件的扩展名
if (type.Equals("jpg") || type.Equals("bmp") || type.Equals("gif") || type.Equals("png") || type.Equals("jpeg"))
{
Random rd = new System.Random();
//string filename = fullfilename.Substring(fullfilename.LastIndexOf("\\") + 1); //上传的文件名
dataUrl = DateTime.Now.ToString("yyy-MM-dd") + rd.Next(10000).ToString() + "." + type; //数据库字段值
string serverpath = System.Web.HttpContext.Current.Server.MapPath(imageUrl) + dataUrl; //上传路径
fileUpName.PostedFile.SaveAs(serverpath);
}
}
return dataUrl;
}
#endregion
#region 上传文件
/// <summary>
/// 上传文件
/// </summary>
/// <param name="fileUpName">FileUpload控件名</param>
/// <param name="fileUrl">上传文件夹路径</param>
/// <param name="dataUrl">插入数据库的路径值</param>
/// <returns></returns>
public static string UpLoadFile(System.Web.UI.WebControls.FileUpload fileUpName, string fileUrl)
{
string dataUrl = "";
if (fileUpName.HasFile)
{
Random rd = new System.Random();
string filepath = fileUpName.PostedFile.FileName; //上传文件 本地 路径
//dataUrl = filepath.Substring(filepath.LastIndexOf("\\") + 1); //上传的文件名
string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1); //上传的文件名
dataUrl = filename; //数据库字段值
string serverpath = System.Web.HttpContext.Current.Server.MapPath(fileUrl) + dataUrl; //上传路径
fileUpName.PostedFile.SaveAs(serverpath);
}
return dataUrl;
}
#endregion
#region 截取字符串
/// <summary>
/// 截取字符串
/// </summary>
/// <param name="strString">字符串</param>
/// <param name="intLength">长度</param>
/// <returns></returns>
public static string SubString(string input, int p)
{
Encoding encode = Encoding.GetEncoding("gb2312");
byte[] byteArr = encode.GetBytes(input);
if (byteArr.Length <= p) return input;
int m = 0, n = 0;
foreach (byte b in byteArr)
{
if (n >= p) break;
if (b > 127) m++; //重要一步:对前p个字节中的值大于127的字符进行统计
n++;
}
if (m % 2 != 0) n = p + 1; //如果非偶:则说明末尾为双字节字符,截取位数加1
return encode.GetString(byteArr, 0, n);
}
#endregion
#region Seo设置
/// <summary>
/// Seo设置
/// </summary>
/// <param name="seoTitle">标题</param>
/// <param name="seoKeyWords">关键字</param>
/// <param name="seoDescription">描述</param>
/// <param name="page"></param>
public static void setMeta(string seoTitle, string seoKeyWords, string seoDescription, Page page)
{
page.Title = seoTitle;
System.Web.UI.HtmlControls.HtmlMeta hmKey = new HtmlMeta();
hmKey.Name = "keywords";
hmKey.Content = seoKeyWords;
System.Web.UI.HtmlControls.HtmlMeta hmDes = new HtmlMeta();
hmDes.Name = "description";
hmDes.Content = seoDescription;
System.Web.UI.Page p = (System.Web.UI.Page)System.Web.HttpContext.Current.Handler;
p.Header.Controls.Add(hmKey);
p.Header.Controls.Add(hmDes);
}
/ <summary>
/ 调用示例
/ </summary>
//public void BindSeo()
//{
// string sql = string.Format("select Title,KeyWord,Contents from T_Seo where id=1");
// DataTable dt = DBHelper.GetDataTable(sql);
// WebUI.setMeta(dt.Rows[0]["Title"].ToString(), dt.Rows[0]["KeyWord"].ToString(), dt.Rows[0]["Contents"].ToString(), this.Page);
//}
#endregion
}