//
// 文件名:Pub.cs
// 文件功能描述:公共函数类
//
//
//
// 修改标识:
// 修改描述:
----------------------------------------------------------------*/
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;
using jmail;
using DAL;
using System.Web.SessionState;
/// <summary>
/// Pub 的摘要说明。
/// </summary>
public class Pub
{
public Pub()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
//数据库连接字符串
//public SqlConnection myConn = new SqlConnection(ConfigurationManager.AppSettings["CycbizTotalConnectString"]);
/// <summary>
/// 利用jmail发送邮件程序
/// </summary>
/// <param name="strSubject">邮件的标题</param>
/// <param name="strContent">邮件的内容</param>
/// <param name="MailServerUserName">用于发送邮件的邮件登录用户名</param>
/// <param name="MailServerPassWord">用于发送邮件的邮件登录密码</param>
/// <param name="Charset">邮件的编码格式</param>
/// <param name="AddRecipient">收件人的email地址</param>
/// <returns>返回ture为发送成功!false为发送失败!</returns>
public bool SendMail(string strSubject, string strContent, string MailServerUserName, string MailServerPassWord, string Charset, string AddRecipient)
{
//发送电子邮件
jmail.MessageClass msgClass = new MessageClass();
jmail.POP3Class pop = new POP3Class();
jmail.Attachment attach = new AttachmentClass();
msgClass.Silent = true;
msgClass.Logging = true;
msgClass.MailServerUserName = MailServerUserName;
msgClass.MailServerPassWord = MailServerPassWord;
msgClass.Subject = strSubject;
msgClass.Body = strContent;
msgClass.From = MailServerUserName;
msgClass.Charset = Charset;
msgClass.ContentType = "text/html";
MailServerUserName = MailServerUserName.Remove(0, MailServerUserName.IndexOf("@"));
//添加收件人
msgClass.AddRecipient(AddRecipient, " ", null);
//取得smtp服务器地址
string SmtpServerName = "smtp." + MailServerUserName.Remove(0, MailServerUserName.IndexOf("@") + 1);
//Jmail发送的方法
bool Result;
try
{
Result = msgClass.Send(SmtpServerName, false);
}
catch
{
Result = false;
}
msgClass.Close();
return Result;
}
#region 是否是数值或正整数
///(IsNumeric():判断给定字符串是否为数值;FormatString():格式化字符串;ToString()还原格式化字符串;CutStr():截取字符串)
///
///
/// <summary>
/// 名称: IsNumeric
/// 作用: 判断给定字符串是否为数值
/// 作者: crq
/// </summary>
/// <param name="Str">需要判定的字符串</param>
/// <returns>bool 值</returns>
public static bool IsNumeric(string Str)
{
return StartIsNum(Str, false);
}
/// <summary>
/// 名称: IsNumeric
/// 作用: 是否需要判定值为"正整数"
/// 作者: crq
/// </summary>
/// <param name="Str">需要判定的字符串</param>
/// <param name="IsInt">是否需要判定值为正整数</param>
/// /// <returns>bool 值</returns>
public static bool IsNumeric(string Str, bool IsInt)
{
return StartIsNum(Str, true);
}
private static bool StartIsNum(string Str, bool IsInt)
{
char sChar;
if (IsInt == true)
{
if (Str == null || Str == "")
{
return false;
}
else
{
for (int i = 0; i < Str.Length; i++)
{
sChar = Str[i];
if (sChar < '0' || sChar > '9') return false;
}
return true;
}
}
else
{
if (Str == null || Str == "" || (Str.IndexOf(".") != (Str.LastIndexOf("."))))
{
return false;
}
else
{
for (int i = 0; i < Str.Length; i++)
{
sChar = Str[i];
if ((sChar < '0' || sChar > '9') && (sChar.ToString() != ".".ToString()))
return false;
}
return true;
}
}
}
#endregion
#region 格式化或还原字符串,截取字符串
/// <summary>
/// 格式化字符串
/// </summary>
/// <param name="Str"></param>
/// <returns></returns>
public static string FormatString(string Str)
{
Str = Str.Replace("&", "&");
Str = Str.Replace(";", "&sem");
Str = Str.Replace("'", "''");
Str = Str.Replace("-", "—");
Str = Str.Replace("<", "<");
Str = Str.Replace(">", ">");
//Str=Str.Replace("/"","");
return Str;
}
/// <summary>
/// 还原格式化字符串
/// </summary>
/// <param name="Str"></param>
/// <returns></returns>
public static string ToString(string Str)
{
Str = Str.Replace("&","&" );
Str = Str.Replace("&sem",";" );
Str = Str.Replace("''","'" );
Str = Str.Replace("—","-" );
Str = Str.Replace("<","<" );
Str = Str.Replace(">",">" );
Str = Str.Replace(";", "");
//Str= HtmlToString(Str);
return Str;
}
/// <summary>
/// 清除HTML函数
/// </summary>
/// <param name="Htmlstring">要过滤的字符串</param>
/// <returns></returns>
public static string NoHTML(string Htmlstring)
{
//删除脚本
//删除HTML
Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "/"", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "/xa1", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "/xa2", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "/xa3", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "/xa9", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&#(/d+);", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"([/r/n])[/s]+", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
//Htmlstring=Htmlstring.Replace("<", "");
//Htmlstring=Htmlstring.Replace(">", "");
Htmlstring=Htmlstring.Replace("/r/n", "");
Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
return Htmlstring;
}
/// <summary>
/// 过滤字符串中的HTML代码
/// </summary>
/// <param name="Str"></param>
/// <returns></returns>
public static string HtmlToString(string Str)
{
return System.Text.RegularExpressions.Regex.Replace( Str, "<[^>]+>" , "");
}
/// <summary>
///作用: 截取字符串
///时间:2005-12-20
///作者:crq
/// </summary>
/// <param name="Str">要处理字符串</param>
/// <param name="StrLen">截取长度</param>
/// <returns></returns>
public static string CutStr(string Str, int StrLen)
{
if (Str != null && Convert.IsDBNull(StrLen) == false)
{
int t = 0;
for (int i = 0; i <= StrLen; i++)
{
if (t >= StrLen)
{
Str = Str.Substring(0,i) + "...";
break;
}
else
{
if (i >= Str.Length)
{
Str = Str.Substring(0,i);
break;
}
}
int c = Math.Abs((int)Str[i]);
if (c > 255)
{
t += 2;
}
else
{
t++;
}
}
}
else
{
Str = null;
}
return Str;
}
public static string getStr(string Str)
{
string TempStr="";
Str=ToString(Str);
int start=0;
int end=-1;
if(Str.IndexOf("<",start)!=-1)
{
while (end!=Str.Length-1)
{
start=Str.IndexOf("<",start);
if(start!=-1)
{
TempStr+=Str.Substring(end+1,start-end-1);
end=Str.IndexOf(">",start);
start=end+1;
}
else
{
TempStr+=Str.Substring(end+1,Str.Length-end-1);
end=Str.Length-1;
}
}
}
else
{
TempStr+=Str;
}
return TempStr;
}
/// <summary>
///作用: 截取字符串无省略号
///时间:2005-12-20
///作者:crq
/// </summary>
/// <param name="Str">要处理字符串</param>
/// <param name="StrLen">截取长度</param>
/// <returns></returns>
public static string CutStrNoPoint(string Str, int StrLen)
{
if (Str != null && Convert.IsDBNull(StrLen) == false)
{
int t = 0;
for (int i = 0; i <= StrLen; i++)
{
if (t >= StrLen)
{
Str = Str.Substring(0, i);
break;
}
else
{
if (i >= Str.Length)
{
Str = Str.Substring(0, i);
break;
}
}
int c = Math.Abs((int)Str[i]);
if (c > 255)
{
t += 2;
}
else
{
t++;
}
}
}
else
{
Str = null;
}
return Str;
}
/// <summary>
/// 传入url地址返回带http://的地址
/// </summary>
/// <param name="StrUrl">传入的url字符串</param>
/// <returns></returns>
public static string FormatUrl(string StrUrl)
{
if(StrUrl.Length>7)
{
if(StrUrl.Substring(0,7)=="http://")
{
return StrUrl;
}
else
{
return "http://"+StrUrl;
}
}
else
{
return "http://"+StrUrl;
}
}
#endregion
#region Alert 或 Confirm (弹出窗口或弹出确认界面)
/// <summary>
///作用:服务器端弹出alert对话框
///时间:2005-12-20
///作者:crq
/// </summary>
/// <param name="str_Message">提示信息,例子:"请输入您姓名!"</param>
/// <param name="page">Page类</param>
public void Alert(string strMessage,Page page)
{
page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('" + strMessage + "');</script>");
}
/// <summary>
///作用:服务器端弹出alert对话框,返回上一页面
///时间:2007-01-20
///作者:crq
/// </summary>
/// <param name="str_Message">提示信息,例子:"请输入您姓名!"</param>
/// <param name="page">Page类</param>
public void Alert(string strMessage, string returnUrl, Page page)
{
page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + strMessage + "');window.location.href='" + returnUrl + "';</script>");
}
/// <summary>
///作用:服务器端弹出alert对话框,新窗口弹出一个页面
///时间:2007-01-20
///作者:crq
/// </summary>
/// <param name="str_Message">提示信息,例子:"请输入您姓名!"</param>
/// <param name="page">Page类</param>
public void AlertTop(string strMessage, string returnUrl, Page page)
{
page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + strMessage + "');top.window.location.href='" + returnUrl + "';</script>");
}
/// <summary>
///作用:服务器端弹出alert对话框,返回上级页面
///时间:2007-01-20
///作者:crq
/// </summary>
/// <param name="str_Message">提示信息,例子:"请输入您姓名!"</param>
/// <param name="page">Page类</param>
public void Alert_return(string strMessage, Page page)
{
page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + strMessage + "');history.go(-1);</script>");
}
/// <summary>
///作用:服务器端弹出alert对话框,然后关闭当前窗口
///时间:2008-07-01
///作者:crq
/// </summary>
/// <param name="str_Message">提示信息,例子:"请输入您姓名!"</param>
/// <param name="page">Page类</param>
public void Alert_Close(string strMessage, Page page)
{
page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + strMessage + "');window.close();</script>");
}
/// <summary>
///作用:服务器端弹出alert对话框,弹出后,聚焦于相应的控件,或选中相应控件
///时间:2005-12-20
///作者:crq
/// </summary>
/// <param name="str_Ctl_Name">获得焦点控件Id值,比如:txt_Name</param>
/// <param name="str_Message">提示信息,例子:"请输入您姓名!"</param>
/// <param name="page">Page类</param>
public void Alert_focus(string strControlName, string strMessage, Page page)
{
page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + strMessage + "');document.forms(0)." + strControlName + ".focus(); document.forms(0)." + strControlName + ".select();</script>");
}
/// <summary>
///作用:服务器端弹出confirm对话框
///时间:2005-12-20
///作者:crq
/// </summary>
/// <param name="str_Message">提示信息,例子:"您是否确认删除!"</param>
/// <param name="page">Page类</param>
public string Confirm(string strMessage, Page page)
{
string returnStr="false";
page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script> if (confirm('" + strMessage + "')==true){returnStr='true';}</script>");
return returnStr;
}
/// <summary>
///作用:服务器端弹出confirm对话框,该函数有个弊端,必须放到响应事件的最后,目前没有妥善解决方案
///时间:2005-12-20
///作者:crq
/// </summary>
/// <param name="str_Message">提示信息,例子:"您是否确认删除!"</param>
/// <param name="btn">隐藏Botton按钮Id值,比如:btn_Flow</param>
/// <param name="page">Page类</param>
public void Confirm(string strMessage, string btn, Page page)
{
page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script> if (confirm('" + strMessage + "')==true){document.forms(0)." + btn + ".click();}</script>");
}
/// <summary>
///作用:服务器端弹出confirm对话框,询问用户准备转向相应操作,包括“确定”和“取消”时的操作
///时间:2005-12-20
///作者:crq
/// </summary>
/// /// <param name="str_Message">提示信息,比如:"成功增加数据,单击/"确定/"按钮填写流程,单击/"取消/"修改数据"</param>
/// <param name="btn_Redirect_Flow">"确定"按钮id值</param>
/// <param name="btn_Redirect_Self">"取消"按钮id值</param>
/// <param name="page">Page类</param>
public void Confirm(string str_Message, string btn_Redirect_Flow, string btn_Redirect_Self, Page page)
{
page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script> if (confirm('" + str_Message + "')==true){document.forms(0)." + btn_Redirect_Flow + ".click();}else{document.forms(0)." + btn_Redirect_Self + ".click();}</script>");
}
#endregion
#region 日期函数(ShowShortDate转换成短型日期,ToWeek求星期几的数值)
/// <summary>
/// 名称: ShowShortDate
/// 作用: 按照yyyy-mm-dd将字符串转换为日期
/// 作者: crq
/// </summary>
/// <param name="Str">需要格式化成指定格式的字符串</param>
/// <returns>格式化后的字符串</returns>
public static string ShowShortDate(string Str)
{
try
{
if (Str == null) Str = "";
if (Str != "")
{
Str = Convert.ToDateTime(Str).ToShortDateString();
if (Str == "1900-1-1")
{
Str = "";
}
}
}
catch
{
Str = "错误格式日期";
}
return Str;
}
/// <summary>
/// 返回参数日期的星期数的值数表示
/// </summary>
/// <param name="dtDate">日期</param>
/// <returns>星期数值表示</returns>
public static int ToWeek(DateTime dtDate)
{
int iWeek = 0;
switch (dtDate.DayOfWeek.ToString().Trim().Substring(0, 3))
{
case "Mon":
iWeek = 1;
break;
case "Tue":
iWeek = 2;
break;
case "Wed":
iWeek = 3;
break;
case "Thu":
iWeek = 4;
break;
case "Fri":
iWeek = 5;
break;
case "Sat":
iWeek = 6;
break;
case "Sun":
iWeek = 7;
break;
}
return iWeek;
}
#endregion
#region 绑定函数 (BindDropDownList, BindCheckBoxList,BindRadioBoxList,ListBox)
/// <summary>
///作用:绑定DropDownList,CheckBoxList,RadioButtonList,ListBox等列表控件
///时间:2005-12-21
///作者:crq
/// </summary>
/// <param name="str_Sql">SQL语句</param>
/// <param name="id">数据库表字段,一般为该表的主键</param>
/// <param name="name">数据库表字段,控将显示值</param>
/// <param name="listctrl">控将ID值</param>
public static void BindCtrl(string id,string name,DataTable dt,Control listctrl)
{
//DataTable dt=new SqlLink().ExeSqlDataTable(str_Sql);
//===DropDownList===
if (listctrl is DropDownList)
{
((DropDownList)listctrl).Items.Clear();
for (int i=0;i<dt.Rows.Count;i++)
{
ListItem it=new ListItem(ToString(dt.Rows[i][name].ToString()),dt.Rows[i][id].ToString());
((DropDownList)listctrl).Items.Add(it);
}
}
//===CheckBoxList===
if (listctrl is CheckBoxList)
{
((CheckBoxList)listctrl).Items.Clear();
for (int i=0;i<dt.Rows.Count;i++)
{
ListItem it=new ListItem(dt.Rows[i][name].ToString(),dt.Rows[i][id].ToString());
((CheckBoxList)listctrl).Items.Add(it);
}
}
//===RadioButtonList===
if (listctrl is RadioButtonList)
{
((RadioButtonList)listctrl).Items.Clear();
for (int i=0;i<dt.Rows.Count;i++)
{
ListItem it=new ListItem(dt.Rows[i][name].ToString(),dt.Rows[i][id].ToString());
((RadioButtonList)listctrl).Items.Add(it);
}
}
//===ListBox===
if (listctrl is ListBox)
{
((ListBox)listctrl).Items.Clear();
for (int i=0;i<dt.Rows.Count;i++)
{
ListItem it=new ListItem(dt.Rows[i][name].ToString(),dt.Rows[i][id].ToString());
((ListBox)listctrl).Items.Add(it);
}
}
dt.Clear();
}
/// <summary>
/// 绑定DROPDROWLIST,默认第一项的值为“”
/// </summary>
/// <param name="id">列表项的值</param>
/// <param name="name">列表项要显示的文字</param>
/// <param name="dt">传递过来的DataTable</param>
/// <param name="listctrl">要绑定的控件的名字</param>
/// <param name="FirstItems">显示在第一项的名字</param>
public static void BindDropDownList(string id, string name, DataTable dt, Control listctrl, string FirstItemsName)
{
((DropDownList)listctrl).Items.Clear();
//DataTable dt=new SqlLink().ExeSqlDataTable(str_Sql);
if (listctrl is DropDownList)
{
ListItem it1 = new ListItem(FirstItemsName, "");
((DropDownList)listctrl).Items.Add(it1);
for (int i = 0; i < dt.Rows.Count; i++)
{
ListItem it = new ListItem(dt.Rows[i][name].ToString(), dt.Rows[i][id].ToString());
((DropDownList)listctrl).Items.Add(it);
}
}
dt.Clear();
}
// 绑定一个日期年的下接菜单
public static void BindDropDownList_Year( DropDownList ddlYear )
{
for ( int i=DateTime.Now.Year-3; i<=DateTime.Now.Year; i++ )
{
ListItem li = new ListItem( i.ToString(), i.ToString() );
if (li.Value == DateTime.Now.Year.ToString())
{
li.Selected = true;
}
ddlYear.Items.Add( li );
}
}
// 绑定一个日期月的下接菜单
public static void BindDropDownList_Month( DropDownList ddlMonth )
{
for ( int i=1; i<=12; i++ )
{
ListItem li = new ListItem( i.ToString(), i.ToString() );
if (li.Value == DateTime.Now.Month.ToString())
{
li.Selected = true;
}
ddlMonth.Items.Add( li );
}
}
// 绑定一个日期日的下接菜单
public static void BindDropDownList_Day( DropDownList ddlDay )
{
for ( int i=1; i<=31; i++ )
{
ListItem li = new ListItem( i.ToString(), i.ToString() );
if (li.Value == DateTime.Now.Day.ToString())
{
li.Selected = true;
}
ddlDay.Items.Add( li );
}
}
#endregion
#region 绑定函数(将DataTable绑定到某个Repeater或者DataList)
public static void BindInfoToControl(Repeater rep, DataTable dt)
{
if (dt.Rows.Count > 0)
{
rep.DataSource = dt;
rep.DataBind();
}
else
{
rep.DataSource = null;
rep.DataBind();
}
}
public static void BindInfoToControl(DataList dl, DataTable dt)
{
if (dt.Rows.Count > 0)
{
dl.DataSource = dt;
dl.DataBind();
}
else
{
dl.DataSource = null;
dl.DataBind();
}
}
#endregion
#region 图片上传
/// <summary>
/// 上传图片
/// </summary>
/// <param name="UploadFile">HtmlInputFile 控件</param>
/// <param name="RelativePath">上传图片的相对路径"../../docuemnt/"</param>
/// <param name="ReLativeExt">允许上传的扩展名集合</param>
/// <param name="strOldName">原来的图片名称</param>
/// <returns></returns>
/// return值为0--文件名为空
/// 为1--不存在该磁盘文件
/// 为2--上传文件过大超出范围
/// 为3--格式不正确
///
public static string UploadImage(System.Web.UI.HtmlControls.HtmlInputFile UploadFile,string RelativePath ,string[] ReLativeExt,string strOldName)
{
HttpPostedFile hpf=UploadFile.PostedFile;
bool IsExt=false;//判断上传图片的扩展名是否在指定范围内
string strFileName=hpf.FileName;
string[] TempName=strFileName.Split('//');
strFileName=TempName[TempName.Length-1].ToString();
if(strFileName.Trim()=="")
return "0";
if(hpf.ContentLength==0)
return "1";
else if(hpf.ContentLength>2097152)
return "2";
string strFileNameExt=System.IO.Path.GetExtension(strFileName.Trim()).ToLower().Trim();
for(int i=0;i<ReLativeExt.Length;i++)
{
if(strFileNameExt==ReLativeExt[i].ToLower())
IsExt=true;
}
if(IsExt==false)
return "3";
//-----------------------------给图片加水印--------------------------------------------
//生成一个暂时的图片文件
string fileName = UploadFile.PostedFile.FileName.Substring(UploadFile.PostedFile.FileName.LastIndexOf('//') + 1);//DateTime.Now.ToString("yyyyMMddhhmmss");
string oldpath = String.Format("{0}{1}", RelativePath, fileName);
UploadFile.PostedFile.SaveAs(HttpContext.Current.Server.MapPath(oldpath));
System.Drawing.Image oldimage = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(RelativePath + fileName));
//加文字水印
Bitmap output = new Bitmap(oldimage);
Graphics g = Graphics.FromImage(output);
g.DrawString("Cycbiz", new Font("Arial", 12), new SolidBrush(Color.White), oldimage.Width - 60, oldimage.Height - 25);
HttpContext.Current.Response.ContentType = "image/gif";
//保存加水印过后的图片,删除原始图片
strFileName = String.Format("{0}{1}", DateTime.Now.Ticks.ToString(), (new Random()).Next(10000, 99999));//DateTime.Now.ToString("yyyyMMddhhmmss");
strFileName += strFileNameExt;
string strUploadPath = String.Format("{0}", strFileName);
string strOldPath = String.Format("{0}{1}", RelativePath, strOldName);
if (System.IO.File.Exists(HttpContext.Current.Server.MapPath(strOldPath)))
{
System.IO.File.Delete(HttpContext.Current.Server.MapPath(strOldPath));
}
output.Save(HttpContext.Current.Server.MapPath(RelativePath + strFileName), System.Drawing.Imaging.ImageFormat.Jpeg);
//image.Save(newPath);
oldimage.Dispose();
//保存加水印过后的图片,删除原始图片
if (File.Exists(HttpContext.Current.Server.MapPath(oldpath)))
{
File.Delete(HttpContext.Current.Server.MapPath(oldpath));
}
//-----------------------------给图片加水印结束--------------------------------------------
return strUploadPath;
//生成上传得文件名
//strFileName=String.Format("{0}{1}", DateTime.Now.Ticks.ToString(), (new Random()).Next(10000, 99999));
//strFileName+=strFileNameExt;
//string strUploadPath= String.Format("{0}{1}", RelativePath, strFileName);
//string strOldPath=String.Format("{0}{1}", RelativePath, strOldName);
//if (System.IO.File.Exists(HttpContext.Current.Server.MapPath(strOldPath)))
//{
// System.IO.File.Delete(HttpContext.Current.Server.MapPath(strOldPath));
//}
//hpf.SaveAs(HttpContext.Current.Server.MapPath(strUploadPath));
//return strFileName;
}
#endregion
/// <summary>
/// 绑定下拉列表框中选定的值
/// </summary>
/// <param name="dr">下拉列表框对象</param>
/// <param name="BindValue">要选中的值</param>
public static void drop(DropDownList dr, string BindValue)
{
for (int i = 0; i < dr.Items.Count; i++)
{
if (dr.Items[i].Value == BindValue)
{
dr.Items[i].Selected = true;
break;
}
}
}
#region 图片上传不加水印
/// <summary>
/// 上传图片不加水印
/// </summary>
/// <param name="UploadFile">HtmlInputFile 控件</param>
/// <param name="RelativePath">上传图片的相对路径"../../docuemnt/"</param>
/// <param name="ReLativeExt">允许上传的扩展名集合</param>
/// <param name="strOldName">原来的图片名称</param>
/// <returns></returns>
/// return值为0--文件名为空
/// 为1--不存在该磁盘文件
/// 为2--上传文件过大超出范围
/// 为3--格式不正确
///
public static string UploadImageNoCyc(System.Web.UI.HtmlControls.HtmlInputFile UploadFile, string RelativePath, string[] ReLativeExt, string strOldName)
{
HttpPostedFile hpf = UploadFile.PostedFile;
bool IsExt = false;//判断上传图片的扩展名是否在指定范围内
string strFileName = hpf.FileName;
string[] TempName = strFileName.Split('//');
strFileName = TempName[TempName.Length - 1].ToString();
if (strFileName.Trim() == "")
return "0";
if (hpf.ContentLength == 0)
return "1";
else if (hpf.ContentLength > 204800)
return "2";
string strFileNameExt = System.IO.Path.GetExtension(strFileName.Trim()).ToLower().Trim();
for (int i = 0; i < ReLativeExt.Length; i++)
{
if (strFileNameExt == ReLativeExt[i].ToLower())
IsExt = true;
}
if (IsExt == false)
return "3";
//生成上传得文件名
strFileName = String.Format("{0}{1}", DateTime.Now.Ticks.ToString(), (new Random()).Next(10000, 99999));
strFileName += strFileNameExt;
string strUploadPath = String.Format("{0}{1}", RelativePath, strFileName);
string strOldPath = String.Format("{0}{1}", RelativePath, strOldName);
if (System.IO.File.Exists(HttpContext.Current.Server.MapPath(strOldPath)))
{
System.IO.File.Delete(HttpContext.Current.Server.MapPath(strOldPath));
}
hpf.SaveAs(HttpContext.Current.Server.MapPath(strUploadPath));
return strFileName;
}
#endregion
#region 文件上传
/// <summary>
/// 上传图片
/// </summary>
/// <param name="UploadFile">HtmlInputFile 控件</param>
/// <param name="RelativePath">上传文件的相对路径"../../docuemnt/"</param>
/// <param name="ReLativeExt">允许上传的扩展名集合</param>
/// <param name="strOldName">原来的文件名称</param>
/// <returns></returns>
/// return值为0--文件名为空
/// 为1--不存在该磁盘文件
/// 为2--上传文件过大超出范围
/// 为3--格式不正确
///
public static string UploadFile(System.Web.UI.HtmlControls.HtmlInputFile UploadFile, string RelativePath, string[] ReLativeExt, string strOldName)
{
HttpPostedFile hpf = UploadFile.PostedFile;
bool IsExt = false;//判断上传文件的扩展名是否在指定范围内
string strFileName = hpf.FileName;
string[] TempName = strFileName.Split('//');
strFileName = TempName[TempName.Length - 1].ToString();
if (strFileName.Trim() == "")
return "0";
if (hpf.ContentLength == 0)
return "1";
else if (hpf.ContentLength > 2097152)
return "2";
string strFileNameExt = System.IO.Path.GetExtension(strFileName.Trim()).ToLower().Trim();
for (int i = 0; i < ReLativeExt.Length; i++)
{
if (strFileNameExt == ReLativeExt[i].ToLower())
IsExt = true;
}
if (IsExt == false)
return "3";
//生成上传得文件名
strFileName = String.Format("{0}{1}", DateTime.Now.Ticks.ToString(), (new Random()).Next(10000, 99999));
strFileName += strFileNameExt;
string strUploadPath = String.Format("{0}{1}", RelativePath, strFileName);
string strOldPath = String.Format("{0}{1}", RelativePath, strOldName);
if (System.IO.File.Exists(HttpContext.Current.Server.MapPath(strOldPath)))
{
System.IO.File.Delete(HttpContext.Current.Server.MapPath(strOldPath));
}
hpf.SaveAs(HttpContext.Current.Server.MapPath(strUploadPath));
return strFileName;
}
#endregion
#region 传入ip地址返回所在的国家
/// <summary>
/// 传入ip地址返回所在的国家
/// </summary>
/// <param name="ip">ip地址</param>
/// <returns>返回该ip所在地区</returns>
public static string ReturnAddressForIP(string ip)
{
string ipstr = "";
//拆分ip地址
string[] iparr = ip.Split('.');
for (int i = 0; i < iparr.Length; i++)
{
switch (iparr[i].Length)
{
case 3:
ipstr = ipstr + iparr[i];
break;
case 2:
ipstr = ipstr + "0" + iparr[i];
break;
case 1:
ipstr = ipstr + "00" + iparr[i];
break;
}
}
string Address = "Unknown";
//查询ip所在的地区
string sql = "select address from ip where startip<" + ipstr + " and endip>" + ipstr;
DataSet ds = new CommonDAL().ExecSql(sql);
if (ds.Tables.Count > 0)
{
if (ds.Tables[0].Rows.Count > 0)
{
Address = ds.Tables[0].Rows[0][0].ToString();
}
}
return Address;
}
#endregion
#region 对系统的公共操作(如记录日志,获得CheckBoxLis等控件选中值, 获得DataGrid等绑定控件选中值)
/// <summary>
/// 说明:记录日志
/// 作用:记录用户的操作,包括操作的用户ID,姓名,操作的客户名称,操作内容
/// 作者:crq
/// 时间:2005-12-21
/// </summary>
/// <param name="UserID">用户ID</param>
/// <param name="UserName">用户姓名</param>
/// <param name="CostrCompany">客户名称</param>
/// <param name="doWhat">操作内容说明</param>
//public static void WriteUserLog(int UserId, int infoId, string doWhat)
//{
// string sLogSql = "Insert into do_info_log(UserId, infoId, doWhat)" +
// "values(" + UserId + ", '" + infoId + "', '" + doWhat + "')";
// new SqlLink().ExeSqlVoid(sLogSql);
//}
/// <summary>
///作用:获得CheckBoxLis,RadioButtonList,ListBox控件所有选中值并以逗号格开,形式:"2,3,4"
/// </summary>
/// <param name="listctrl">CheckBoxLis,ListBox控件</param>
/// <param name="type">类型,是想获得Value还是Text值</param>
/// <returns></returns>
public static string GetCtrlSeleteValue(Control listctrl,string type)
{
string str_Value="";
switch (type)
{
//===要选择的是value===
case "value":
{
//===CheckBoxList===
if (listctrl is CheckBoxList)
{
for (int i=0;i<((CheckBoxList)listctrl).Items.Count;i++)
{
if (((CheckBoxList)listctrl).Items[i].Selected==true)
{
str_Value=str_Value+((CheckBoxList)listctrl).Items[i].Value+",";
}
}
if (str_Value!="")
{
str_Value=str_Value.Substring(0,str_Value.Length-1);
}
}
//===ListBox===
if (listctrl is ListBox)
{
for (int i=0;i<((ListBox)listctrl).Items.Count;i++)
{
if (((ListBox)listctrl).Items[i].Selected==true)
{
str_Value=str_Value+((ListBox)listctrl).Items[i].Value+",";
}
}
if (str_Value!="")
{
str_Value=str_Value.Substring(0,str_Value.Length-1);
}
}
break;
}
//===要选择的是Text===
case "text":
{
//===CheckBoxList===
if (listctrl is CheckBoxList)
{
for (int i=0;i<((CheckBoxList)listctrl).Items.Count;i++)
{
if (((CheckBoxList)listctrl).Items[i].Selected==true)
{
str_Value=str_Value+((CheckBoxList)listctrl).Items[i].Text+",";
}
}
if (str_Value!="")
{
str_Value=str_Value.Substring(0,str_Value.Length-1);
}
}
//===ListBox===
if (listctrl is ListBox)
{
for (int i=0;i<((ListBox)listctrl).Items.Count;i++)
{
if (((ListBox)listctrl).Items[i].Selected==true)
{
str_Value=str_Value+((ListBox)listctrl).Items[i].Text+",";
}
}
if (str_Value!="")
{
str_Value=str_Value.Substring(0,str_Value.Length-1);
}
}
break;
}
}
return str_Value;
}
/// <summary>
///作用:获得Repeater,DataGrid,DataList控件所有选中Id值,形式"id=1 and id=3"
/// </summary>
/// <param name="id">表的标识字段,比如:"id"</param>
/// <param name="cb_Id">复选框Id,比如"cb_Id"</param>
/// <param name="lbl_Id">Label的Id,比如"lbl_Id"</param>
/// <param name="ctrl">Repeater,DataGrid,DataList控件Id值</param>
/// <param name="Type">类型,标识字段分string,int两种类型</param>
/// <returns></returns>
public static string GetCtrlId(string id,string cb_Id,string lbl_Id,Control ctrl,string Type)
{
string str_Where="";
int j=1;
if (Type=="string")
{
//===Repeater===
if (ctrl is Repeater)
{
for (int i=0;i<((Repeater)ctrl).Items.Count;i++)
{
CheckBox myCheckBox;
Label myLabel;
myCheckBox=(CheckBox)((Repeater)ctrl).Items[i].FindControl(cb_Id);
myLabel=(Label)((Repeater)ctrl).Items[i].FindControl(lbl_Id);
if(myCheckBox.Checked==true)
{
if (j==1)
{
str_Where=id+"='"+myLabel.Text+"'";
//str_Where=id+" in ('"+myLabel.Text+"' ";
}
else
{
str_Where=str_Where+" or "+id+"='"+myLabel.Text+"'";
//str_Where+=" , '"+myLabel.Text+"'";
}
j=j+1;
}
}
//str_where += ")";
}
//===DataList===
if (ctrl is DataList)
{
for (int i=0;i<((DataList)ctrl).Items.Count;i++)
{
CheckBox myCheckBox;
Label myLabel;
myCheckBox=(CheckBox)((DataList)ctrl).Items[i].FindControl(cb_Id);
myLabel=(Label)((DataList)ctrl).Items[i].FindControl(lbl_Id);
if(myCheckBox.Checked==true)
{
if (j==1)
{
str_Where=id+"='"+myLabel.Text+"'";
}
else
{
str_Where=str_Where+" or "+id+"='"+myLabel.Text+"'";
}
j=j+1;
}
}
}
//===DataGrid===
if (ctrl is DataGrid)
{
for (int i=0;i<((DataGrid)ctrl).Items.Count;i++)
{
CheckBox myCheckBox;
Label myLabel;
myCheckBox=(CheckBox)((DataGrid)ctrl).Items[i].FindControl(cb_Id);
myLabel=(Label)((DataGrid)ctrl).Items[i].FindControl(lbl_Id);
if(myCheckBox.Checked==true)
{
if (j==1)
{
str_Where=id+"='"+myLabel.Text+"'";
}
else
{
str_Where=str_Where+" or "+id+"='"+myLabel.Text+"'";
}
j=j+1;
}
}
}
}
if (Type=="int")
{
//===Repeater===
if (ctrl is Repeater)
{
for (int i=0;i<((Repeater)ctrl).Items.Count;i++)
{
CheckBox myCheckBox;
Label myLabel;
myCheckBox=(CheckBox)((Repeater)ctrl).Items[i].FindControl(cb_Id);
myLabel=(Label)((Repeater)ctrl).Items[i].FindControl(lbl_Id);
if(myCheckBox.Checked==true)
{
if (j==1)
{
str_Where=id+"="+myLabel.Text;
}
else
{
str_Where=str_Where+" or "+id+"="+myLabel.Text;
}
j=j+1;
}
}
}
//===DataList===
if (ctrl is DataList)
{
for (int i=0;i<((DataList)ctrl).Items.Count;i++)
{
CheckBox myCheckBox;
Label myLabel;
myCheckBox=(CheckBox)((DataList)ctrl).Items[i].FindControl(cb_Id);
myLabel=(Label)((DataList)ctrl).Items[i].FindControl(lbl_Id);
if(myCheckBox.Checked==true)
{
if (j==1)
{
str_Where=id+"="+myLabel.Text;
}
else
{
str_Where=str_Where+" or "+id+"="+myLabel.Text;
}
j=j+1;
}
}
}
//===DataGrid===
if (ctrl is DataGrid)
{
for (int i=0;i<((DataGrid)ctrl).Items.Count;i++)
{
CheckBox myCheckBox;
Label myLabel;
myCheckBox=(CheckBox)((DataGrid)ctrl).Items[i].FindControl(cb_Id);
myLabel=(Label)((DataGrid)ctrl).Items[i].FindControl(lbl_Id);
if(myCheckBox.Checked==true)
{
if (j==1)
{
str_Where=id+"="+myLabel.Text;
}
else
{
str_Where=str_Where+" or "+id+"="+myLabel.Text;
}
j=j+1;
}
}
}
}
return str_Where;
}
#endregion
#region 根据输入的字符串返回中文字符的数量
/// <summary>
/// 根据输入的字符串返回中文字符的数量
/// </summary>
/// <param name="word">输入的字符串</param>
/// <returns>返回字符串的长度</returns>
public string GetWordLength(string word)
{
int num = 0;
for (int i = 0; i < word.Length; i++)
{
Regex rx = new Regex("^[/u4e00-/u9fa5]$");
if (rx.IsMatch(word[i].ToString()))
{
num++;// 是
}
}
return num.ToString();
}
#endregion
/// <summary>
/// 显示公告信息
/// </summary>
/// <param name="tableName"></param>
/// <param name="sql"></param>
/// <param name="cellCount"></param>
/// <param name="strUrl"></param>
/// <param name="imagePath"></param>
/// <param name="isShowDate"></param>
/// <param name="dateField"></param>
/// <param name="showField"></param>
/// <param name="strLength"></param>
public void ShowNotice(HtmlTable tableName, string sql, int cellCount, string strUrl, string imagePath, bool isShowDate, string dateField, string showField, int strLength)
{
tableName.Rows.Clear();
DataSet ds = new SQLHelper().ExecSqlDataSetCycmedu(sql);
int recordCount = ds.Tables[0].Rows.Count;
HtmlTableRow tr = new HtmlTableRow();
HtmlTableCell tc;
int j = 1;
for (int i = 0; i < recordCount; i++)
{
if (j > cellCount)
{
tableName.Rows.Add(tr);
tr = new HtmlTableRow();
j = 1;
}
tr.Height = "20";
tr.VAlign = "top";
tc = new HtmlTableCell();
tc.Width = "14.5";
tc.Align = "center";
tc.VAlign = "middle";
HtmlImage img = new HtmlImage();
img.Src = imagePath;
tc.Controls.Add(img);
tr.Cells.Add(tc);
tc = new HtmlTableCell();
tc.VAlign = "middle";
Label lb = new Label();
string title = "";
if (strLength == 0)
{
title = ToString(ds.Tables[0].Rows[i][showField].ToString());
}
else
{
title = CutStr(ds.Tables[0].Rows[i][showField].ToString(), strLength);
}
if (ds.Tables[0].Rows[i]["is_recomment"].ToString() == "1")
{
lb.Text = " <a class='bb' href='" + strUrl + "?id=" + ds.Tables[0].Rows[i]["id"] + "' target='_blank' title='" + ds.Tables[0].Rows[i]["title"] + "'>" + "<font color='#cc6600'>" + title + "</font>" + "</a>";
//lb.ForeColor="#ff0000";
}
else
{
lb.Text = " <a class='bb' href='" + strUrl + "?id=" + ds.Tables[0].Rows[i]["id"] + "' target='_blank' title='" + ds.Tables[0].Rows[i]["title"] + "'>" + title + "</a>";
}
tc.Controls.Add(lb);
tr.Cells.Add(tc);
if (isShowDate == true)
{
tc = new HtmlTableCell();
tc.Width = "30%";
lb = new Label();
lb.Text = "[" + ShowShortDate(ds.Tables[0].Rows[i][dateField].ToString()) + "]";
tc.Controls.Add(lb);
tr.Cells.Add(tc);
}
j = j + 1;
}
tableName.Rows.Add(tr);
}
}
文件功能描述:公共函数类
最新推荐文章于 2023-04-19 15:47:41 发布