using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Text;
///
/// Summary
///
public sealed class zj_static
{
public static readonly SqlConnection SqlConnDB = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"].ToString());
// VideoConnDB = new SqlConnection(ConfigurationManager.AppSettings["VideoConnDB"].ToString());
private zj_static()
{
}
//获得页面文件名 Request.Url.Segments[Request.Url.Segments.Length - 1]
#region 页面文件名
///
/// 页面文件名
///
/// 当前页面的文件名
public static string pageFileName()
{
Uri urlstr = HttpContext.Current.Request.Url;
return urlstr.Segments[urlstr.Segments.Length - 1];
}
#endregion
#region 完整url路径
///
/// 完整url路径
///
///
完整url路径
public static string urlFullPath()
{
string tmpstr = HttpContext.Current.Request.Url.ToString();
return tmpstr;
}
#endregion
#region url路径不包括参数
///
/// url路径不包括参数
///
///
url路径不包括参数
public static string urlPath()
{
string tmpstr = urlFullPath();
if(tmpstr.IndexOf("?")>=0)
{
return tmpstr.Substring(0,tmpstr.IndexOf("?"));
}
return tmpstr;
}
#endregion
#region 路径url 防注入函数
///
/// 防SQL注入验证函数
///
///
///
public static void Url_Sql_Pass(string keywordstr)
{
string urlkey;
urlkey = ConfigurationManager.AppSettings["Sql_Keyword"].ToString();
if (!String.IsNullOrEmpty(keywordstr))
{
string[] b;
b = urlkey.Split("|".ToCharArray());
for (int i = 0; i < b.Length; i++)
{
if (keywordstr.IndexOf(b[i].ToString().Trim()) >= 0)
{
ScriptHelper.EndAlert("传递参数中有非法字符[" + b[i].ToString().Trim() + "]!", " http://www.ca800.com");
break;
}
}
}
}
#endregion
#region 字符串截取函数
///
/// 字符串截取函数
///
///
///
///
public static string cutString(string str, int len)
{
int p_num = 0;
int i;
string tmpStr = "";
if (str == "")
{
tmpStr = "";
}
else
{
int Len_Num = str.Length;
for (i = 0; i <= Len_Num - 1; i++)
{
if (i > Len_Num) break;
char c = Convert.ToChar(str.Substring(i, 1));
if (((int)c > 255) || ((int)c < 0))
{
p_num = p_num + 2;
}
else
{
p_num = p_num + 1;
}
if (p_num >= len)
{
tmpStr = str.Substring(0, i + 1);
return tmpStr;
}
}
}
return str;
}
#endregion
#region 获得某字段的值函数
///
/// 获得某字段的值函数
///
///
///
///
public static string Get_Filed_Value(string sql, string filedname)
{
string returnstr;
returnstr = "";
try
{
SqlCommand Comm = new SqlCommand(sql, SqlConnDB);
SqlConnDB.Open();
SqlDataReader Dr = Comm.ExecuteReader();
if (Dr.Read())
{
returnstr = Dr[filedname].ToString();
}
SqlConnDB.Close();
}
catch (Exception e1)
{
returnstr = "error:"+ e1.Message;
SqlConnDB.Close();
}
finally
{
}
return returnstr;
}
#endregion
#region 获得数据集函数
///
/// 获得数据集函数
///
///
///
public DataSet Return_DataSet(string sql)
{
DataSet ds = new DataSet();
try
{
SqlDataAdapter da = new SqlDataAdapter(sql, SqlConnDB);
SqlConnDB.Open();
da.Fill(ds, "Table1");
SqlConnDB.Close();
}
catch
{
SqlConnDB.Close();
}
return ds;
}
#endregion
#region 绑定数据到控件
#region 绑定多选框数据集(dataset)函数
///
/// 多选框数据集(dataset)绑定函数
///
///
///
public static void Bind_Data_CBL(CheckBoxList CBL, DataSet ds)
{
CBL.Items.Clear();
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
CBL.Items.Add(i.ToString());
CBL.Items[i].Text = ds.Tables[0].Rows[i]["text"].ToString();
CBL.Items[i].Value = ds.Tables[0].Rows[i]["value"].ToString();
}
}
#endregion
#region 绑定下拉列表数据函数
///
/// 绑定下拉列表数据(listItem)函数
///
///
///
public static void Bind_Data_DDL(DropDownList DDL, string sql)
{
try
{
DDL.Items.Clear();
DDL.Items.Add("0");
DDL.Items[0].Text = "请选择";
DDL.Items[0].Value = "0";
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sql, SqlConnDB);
SqlConnDB.Open();
da.Fill(ds, "Table1");
SqlConnDB.Close();
for (int i = 1; i < ds.Tables["Table1"].Rows.Count + 1; i++)
{
DDL.Items.Add(i.ToString());
DDL.Items[i].Text = ds.Tables["Table1"].Rows[i - 1]["text"].ToString();
DDL.Items[i].Value = ds.Tables["Table1"].Rows[i - 1]["value"].ToString();
}
ds.Reset();
}
catch
{
SqlConnDB.Close();
}
}
///
/// 绑定下拉列表数据(ListItem[])函数
///
///
///
public static void Bind_Data_DDL(DropDownList DDL, ListItem[] li)
{
DDL.Items.Clear();
for (int i = 0; i < li.Length; i++)
DDL.Items.Add(li[i]);
}
///
/// 绑定下拉列表数据集(dataset)函数
///
///
///
public static void Bind_Data_DDL(DropDownList DDL, DataSet ds)
{
DDL.Items.Clear();
DDL.Items.Add("0");
DDL.Items[0].Text = "请选择";
DDL.Items[0].Value = "0";
for (int i = 1; i < ds.Tables["Table1"].Rows.Count + 1; i++)
{
DDL.Items.Add(i.ToString());
DDL.Items[i].Text = ds.Tables["Table1"].Rows[i - 1]["text"].ToString();
DDL.Items[i].Value = ds.Tables["Table1"].Rows[i - 1]["value"].ToString();
}
}
#endregion
#region 绑定单选框list函数
///
/// 绑定单选框数据(sql)函数
///
///
///
///
public static void Bind_Data_RBL(RadioButtonList RBL, string sql)
{
try
{
RBL.Items.Clear();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sql, SqlConnDB);
SqlConnDB.Open();
da.Fill(ds, "Table1");
SqlConnDB.Close();
for (int i = 0; i < ds.Tables["Table1"].Rows.Count; i++)
{
RBL.Items.Add(i.ToString());
RBL.Items[i].Text = ds.Tables["Table1"].Rows[i]["text"].ToString();
RBL.Items[i].Value = ds.Tables["Table1"].Rows[i]["value"].ToString();
}
ds.Reset();
}
catch
{
SqlConnDB.Close();
}
}
///
/// 绑定单选框数据集(dataset)函数
///
///
///
public static void Bind_Data_RBL(RadioButtonList RBL, DataSet ds)
{
RBL.Items.Clear();
for (int i = 0; i < ds.Tables["Table1"].Rows.Count; i++)
{
RBL.Items.Add(i.ToString());
RBL.Items[i].Text = ds.Tables["Table1"].Rows[i]["text"].ToString();
RBL.Items[i].Value = ds.Tables["Table1"].Rows[i]["value"].ToString();
}
}
///
/// 单选框数据(listItem)绑定函数
///
///
///
///
public static void Bind_Data_RBL(RadioButtonList RBL, ListItem[] li)
{
RBL.Items.Clear();
for (int i = 0; i < li.Length; i++)
{
RBL.Items.Add(li[i]);
}
}
#endregion
#endregion
#region 验证多选框数否选中 //单选可以用验证控件requirefieldvalidate
///
///验证多选框数否选中 //单选可以用验证控件requirefieldvalidate
///
///
///
public static Boolean IsChoosed_CBL(CheckBoxList tmp)
{
bool isChoosed = false;
for (int i = 0, len = tmp.Items.Count; i < len; i++)
{
if (tmp.Items[i].Selected)
{
isChoosed = true;
}
}
return isChoosed;
}
#endregion
#region 初始化控件到指定的数据---选中状态
///
/// 初始化下拉列表函数
///
///
///
public static void Set_DDL_List(DropDownList ddl, string str)
{
if (!String.IsNullOrEmpty(str))
{
for (int i = 0; i < ddl.Items.Count; i++)
{
if (ddl.Items[i].Value.Trim() == str.Trim())
{
ddl.Items[i].Selected = true;
ddl.Items[0].Selected = false;
i = ddl.Items.Count;
}
}
}
}
///
/// 初始化单选框列表函数
///
///
///
public static void Set_RBL_List(RadioButtonList rbl, string str)
{
if (!String.IsNullOrEmpty(str))
{
if (str.Trim().Length > 0)
{
for (int i = 0; i < rbl.Items.Count; i++)
{
if (rbl.Items[i].Value.Trim() == str.Trim())
{
rbl.Items[i].Selected = true;
i = rbl.Items.Count;
}
}
}
}
}
///
/// 初始化多选框列表函数
///
///
///
public static void Set_CBL_List(CheckBoxList cbl, string str)
{
if (!String.IsNullOrEmpty(str))
{
string[] list;
int i, j;
list = str.Split(",".ToCharArray());
for (j = 0; j < list.Length; j++)
{
for (i = 0; i < cbl.Items.Count; i++)
{
if (cbl.Items[i].Value.Trim() == list[j].Trim())
{
cbl.Items[i].Selected = true;
i = cbl.Items.Count;
}
}
}
}
}
#endregion
#region 获得MD5加密字符串
///
/// 获得MD5加密字符串
///
///
/// 加密字符串
public static string Get_MD5_Method(string strSource)
{
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strSource, "MD5").ToLower().Substring(8, 16);
}
#endregion
#region 把JavaScript的escape()转换过去的字符串解释回来
///
/// eacape加密
///
///
///
public static string escape(string s)
{
StringBuilder sb = new StringBuilder();
byte[] byteArr = System.Text.Encoding.Unicode.GetBytes(s);
for (int i = 0; i < byteArr.Length; i += 2)
{
sb.Append("%u");
sb.Append(byteArr[i + 1].ToString("X2"));//把字节转换为十六进制的字符串表现形式
sb.Append(byteArr[i].ToString("X2"));
}
return sb.ToString();
}
///
/// 把JavaScript的escape()转换过去的字符串解释回来些方法支持汉字
///
///
///
public static string unescape(string s)
{
string str = s.Remove(0, 2);//删除最前面两个"%u"
string[] strArr = str.Split(new string[] { "%u" }, StringSplitOptions.None);//以子字符串"%u"分隔
byte[] byteArr = new byte[strArr.Length * 2];
for (int i = 0, j = 0; i < strArr.Length; i++, j += 2)
{
byteArr[j + 1] = Convert.ToByte(strArr[i].Substring(0, 2), 16); //把十六进制形式的字串符串转换为二进制字节
byteArr[j] = Convert.ToByte(strArr[i].Substring(2, 2), 16);
}
str = System.Text.Encoding.Unicode.GetString(byteArr); //把字节转为unicode编码
return str;
}
#endregion
#region Cookies 操作 ------ set get del
///
/// 创建Cookies
///
/// Cookie 主键
/// Cookie 键值
/// Cookie 天数
///
Cookie ck = new Cookie();
///
ck.setCookie("主键","键值","天数");
public static bool SetCookie(string strName, string strValue, int strDay)
{
try
{
strValue = HttpContext.Current.Server.UrlEncode(strValue);
HttpCookie Cookie = new HttpCookie(strName);
Cookie.Expires = DateTime.Now.AddDays(strDay);
Cookie.Value = strValue;
System.Web.HttpContext.Current.Response.Cookies.Add(Cookie);
return true;
}
catch
{
return false;
}
}
/**/
///
/// 读取Cookies
///
/// Cookie 主键
///
Cookie ck = new Cookie();
///
ck.getCookie("主键");
public static string GetCookie(string strName)
{
HttpCookie Cookie = System.Web.HttpContext.Current.Request.Cookies[strName];
if (Cookie != null)
{
if (Cookie.Value != null)
{
return HttpContext.Current.Server.UrlDecode(Cookie.Value.ToString());
}
else
{
return null;
}
}
else
{
return null;
}
}
/**/
///
/// 删除Cookies
///
/// Cookie 主键
///
Cookie ck = new Cookie();
///
ck.delCookie("主键");
public static bool DelCookie(string strName)
{
try
{
HttpCookie Cookie = new HttpCookie(strName);
Cookie.Expires = DateTime.Now.AddDays(-1);
System.Web.HttpContext.Current.Response.Cookies.Add(Cookie);
return true;
}
catch
{
return false;
}
}
#endregion
///
/// /// Code By MuseStudio@hotmail.com
/// 2004-11-30
///
/// ///
public string GetPYChar(string c)
{
byte[] array = new byte[2];
array = System.Text.Encoding.Default.GetBytes(c);
int i = (short)(array[0] - '/0') * 256 + ((short)(array[1] - '/0'));
if ( i < 0xB0A1) return "*";
if ( i < 0xB0C5) return "a";
if ( i < 0xB2C1) return "b";
if ( i < 0xB4EE) return "c";
if ( i < 0xB6EA) return "d";
if ( i < 0xB7A2) return "e";
if ( i < 0xB8C1) return "f";
if ( i < 0xB9FE) return "g";
if ( i < 0xBBF7) return "h";
if ( i < 0xBFA6) return "j";
if ( i < 0xC0AC) return "k";
if ( i < 0xC2E8) return "l";
if ( i < 0xC4C3) return "m";
if ( i < 0xC5B6) return "n";
if ( i < 0xC5BE) return "o";
if ( i < 0xC6DA) return "p";
if ( i < 0xC8BB) return "q";
if ( i < 0xC8F6) return "r";
if ( i < 0xCBFA) return "s";
if ( i < 0xCDDA) return "t";
if ( i < 0xCEF4) return "w";
if ( i < 0xD1B9) return "x";
if ( i < 0xD4D1) return "y";
if ( i < 0xD7FA) return "z";
return "*";
}
///
/// 转全角的函数(SBC case)
///
/// 任意字符串
/// 全角字符串
///
///全角空格为12288,半角空格为32
///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
///
public string ToSBC(string input)
{
//半角转全角:
char[] c = input.ToCharArray();
for (int i = 0; i < c.Length; i++)
{
if (c[i] == 32)
{
c[i] = (char)12288;
continue;
}
if (c[i] < 127)
c[i] = (char)(c[i] + 65248);
}
return new string(c);
}
///
/// 转半角的函数(DBC case)
///
/// 任意字符串
/// 半角字符串
///
///全角空格为12288,半角空格为32
///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
///
public string ToDBC(string input)
{
char[] c = input.ToCharArray();
for (int i = 0; i < c.Length; i++)
{
if (c[i] == 12288)
{
c[i] = (char)32;
continue;
}
if (c[i] > 65280 && c[i] < 65375)
c[i] = (char)(c[i] - 65248);
}
return new string(c);
}
}