代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Asp.Net
】
asp.net前台常用方法
作者:
wd63575706
/ 发布于
2012/3/19
/
678
<div>using System; using System.Web ; using System.Data; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.Configuration; using Tangsoft.Business; namespace Biz { /// <summary> /// 用于前台的通用方法 /// </summary> public class CommonMethod { public CommonMethod() { // // TODO: 在此处添加构造函数逻辑 // } private static string id; public static string AgentCompanyId { set{id=value;} get{return id;} } /// <summary> /// 截取字符串 /// </summary> /// <remarks>可用于DataList等控件绑定显示</remarks> /// <param name="obj"></param> /// <param name="length">长度</param> /// <returns>截取后的字符串</returns> public static string CutString(Object obj,int length) { string str = obj.ToString(); str = HttpUtility.HtmlDecode(str); int i = 0, j = 0; foreach(char chr in str) { if((int)chr > 127) { i += 2; } else { i ++; } if (i > length) { str = str.Substring(0, j) + "..."; break; } j ++; } return str; } /// <summary> /// 转化为STR类型 /// </summary> /// <param name="obj"></param> /// <returns></returns> public static string ToStr(object obj) { try { return Convert.ToString(obj); } catch { return""; } } public static string FixWebLink(string source) { if(source.StartsWith("http")) { return source; } else { return @"<a href="http://"+source">http://"+source</a>; } } /// <summary> /// 转化为Int类型 /// </summary> /// <param name="obj"></param> /// <returns></returns> public static int ToInt(object obj) { try { return Convert.ToInt32(obj); } catch { return 0; } } /// <summary> /// 转化为Int类型 /// </summary> /// <param name="obj"></param> /// <returns></returns> public static float ToFloat(object obj) { try { return (float)Convert.ToDouble(obj); } catch { return 0; } } /// <summary> /// 转化为Int类型 /// </summary> /// <param name="obj"></param> /// <returns></returns> public static string ToShortDate(object obj) { try { return Convert.ToDateTime(obj).ToString("yy-MM-dd"); } catch { return ""; } } /// <summary> /// 去掉数字小数点后面末尾的0 /// </summary> /// <param name="obj"></param> /// <returns></returns> public static string TrimEnd0(object obj) { string str = ""; try { str = obj.ToString(); } catch { return "0"; } if(str.IndexOf(".")>0) { str = str.TrimEnd('0').TrimEnd('.'); } return str; } /// <summary> /// 列表显示新闻标题,颜色处理 /// </summary> /// <param name="obj"></param> /// <param name="length"></param> /// <param name="objColor"></param> /// <returns></returns> public static string TitleColored(Object obj,int length,Object objColor) { string str=CutString(obj,length); if(objColor.ToString().Length>0) { str="<font color="+objColor.ToString()+">"+str+"</font>"; } return str; } public static string FillData(string source,string message) { return source.Length<1?message:source; } /// <summary> /// 截取字符串,不显示省略号 /// </summary> /// <remarks>可用于DataList等控件绑定显示</remarks> /// <param name="obj"></param> /// <param name="length">长度</param> /// <returns>截取后的字符串</returns> public static string CutOnly(Object obj,int length) { string str = obj.ToString(); str = HttpUtility.HtmlDecode(str); int i = 0, j = 0; foreach(char chr in str) { if((int)chr > 127) { i += 2; } else { i ++; } if (i > length) { str = str.Substring(0, j) ; break; } j ++; } return str; } /// <summary> /// 截取字符串,不显示省略号 /// </summary> /// <remarks>可用于DataList等控件绑定显示</remarks> /// <param name="obj"></param> /// <param name="length">长度</param> /// <returns>截取后的字符串</returns> public static string AddOnly(Object obj,int length) { string str = obj.ToString(); str = HttpUtility.HtmlDecode(str); int i = 0, j = 0; foreach(char chr in str) { if((int)chr > 127) { i += 2; } else { i ++; } j ++; } if(i<length) { for(int k=i;k<=length;k++) { str = str + " "; } } return str; } /// <summary> /// 设置显示的日期字段为短日期字符串 /// </summary> /// <remarks>在控件绑定值后调用</remarks> /// <param name="hgc"></param> public static void ShortDate(HtmlGenericControl hgc) { try { string str = hgc.InnerHtml.ToString(); hgc.InnerHtml = DateTime.Parse(str).ToShortDateString(); } catch { } } /// <summary> /// 设置显示的日期字段为短日期字符串 /// </summary> /// <remarks>在控件绑定值后调用</remarks> /// <param name="hgc"></param> public static void ShortDate(Label lbl) { try { string str = lbl.Text.ToString(); lbl.Text = DateTime.Parse(str).ToShortDateString(); } catch { } } /// <summary> /// 进行换行 /// </summary> /// <param name="strOld"></param> /// <param name="strRepace"></param> /// <returns></returns> public static string AddBR(Object obj,string strRepace) { string strOld = obj.ToString(); string strNew = strOld; if(strRepace=="·") { strNew = strNew.Replace("·","·"); strNew = strNew.Replace("·","·"); } else if ( strRepace=="·") { strNew = strNew.Replace("·","·"); strNew = strNew.Replace("·","·"); } else { strNew = strOld.Replace(strRepace,""); } return strNew; } /// <summary> /// 替换空格显示 /// </summary> /// <param name="hgc"></param> public static void ReplaceSpace(HtmlGenericControl hgc) { hgc.InnerHtml = hgc.InnerHtml.Replace(" "," "); } /// <summary> /// 绑定Repeater /// </summary> /// <param name="repeater"></param> /// <param name="ds"></param> public static void BindRepeater(Repeater repeater,DataSet ds) { if( ds!=null ) { repeater.DataSource = ConverDsForPage(ds); repeater.DataMember = ds.Tables[0].TableName ; repeater.DataBind(); } } /// <summary> /// /// </summary> /// <param name="repeater"></param> /// <param name="dt"></param> public static void BindRepeater(Repeater repeater,DataTable dt) { if( dt!=null ) { DataSet ds = new DataSet(); ds.Tables.Add(dt); BindRepeater(repeater,ds); } } /// <summary> /// 绑定DataList /// </summary> /// <param name="dataList"></param> /// <param name="ds"></param> public static void BindDataList(DataList dataList,DataSet ds) { if( ds!=null ) { dataList.DataSource = ds; dataList.DataMember = ds.Tables[0].TableName ; dataList.DataBind(); } } /// <summary> /// 绑定DataList /// </summary> /// <param name="dataList"></param> /// <param name="dt"></param> public static void BindDataList(DataList dataList,DataTable dt) { if( dt!=null ) { dataList.DataSource = dt; dataList.DataMember = dt.TableName ; dataList.DataBind(); } } /// <summary> /// 数据库的DataSet转换为页面显示的格式 /// </summary> /// <param name="sDs"></param> /// <returns></returns> public static DataSet ConverDsForPage(DataSet sDs) { DataSet aimDs = new DataSet(); DataTable aimDt = new DataTable(); if(sDs!=null) { aimDt.TableName = sDs.Tables[0].TableName; //结构 for(int i=0;i<sDs.Tables[0].Columns.Count;i++) { DataColumn aimDc ; aimDc = new DataColumn(sDs.Tables[0].Columns[i].ColumnName,System.Type.GetType("System.String")); aimDt.Columns.Add(aimDc); } //数据 for(int i=0;i<sDs.Tables[0].Rows.Count;i++) { DataRow aimDr; aimDr = aimDt.NewRow(); for(int j=0;j<sDs.Tables[0].Columns.Count;j++) { string tmp_sValue = sDs.Tables[0].Rows[i][j].ToString(); if(tmp_sValue == "0") { aimDr[j] = ""; } else if (sDs.Tables[0].Columns[j].DataType .ToString () == "System.DateTime") { try { if(DateTime.Parse (sDs.Tables[0].Rows[i][j].ToString()).Year != 1900) { aimDr[j] = DateTime.Parse(sDs.Tables[0].Rows[i][j].ToString()).ToString("yyyy-MM-dd"); } else aimDr[j] = "暂未提供"; } catch { aimDr[j] = "暂未提供"; } } else if(sDs.Tables[0].Columns[j].DataType .ToString () == "System.Decimal") { if(sDs.Tables[0].Rows[i][j].ToString().IndexOf('.')>0) { aimDr[j] = sDs.Tables[0].Rows[i][j].ToString().TrimEnd('0').TrimEnd('.'); } else { aimDr[j] = sDs.Tables[0].Rows[i][j].ToString(); } } //else if (sDs.Tables[0].Columns[j].ColumnName.ToLower() == "outerarea" //|| sDs.Tables[0].Columns[j].ColumnName.ToLower() == "gardenarea" //|| sDs.Tables[0].Columns[j].ColumnName.ToLower() == "innerarea" //|| sDs.Tables[0].Columns[j].ColumnName.ToLower() == "area" //|| sDs.Tables[0].Columns[j].ColumnName.ToLower() == "startsalearea" //) //{ //try //{ //int tmp_split = tmp_sValue.IndexOf('.'); //if(tmp_split==-1) //{ //aimDr[j] = tmp_sValue; //} //else //{ //aimDr[j] = tmp_sValue.Substring(0,tmp_split); //} //} //catch //{ //aimDr[j] = ""; //} //} else { aimDr[j] = tmp_sValue; } } aimDt.Rows.Add(aimDr); } } aimDs.Tables.Add(aimDt); return aimDs; } /// <summary> /// 数据集列统一加后缀 /// </summary> /// <param name="ds"></param> /// <param name="columnName"></param> /// <param name="postFix"></param> /// <returns></returns> public static DataSet ConvertDsAddPostfix(DataSet ds,string columnName,string postFix) { int m_Count=ds.Tables[0].Rows.Count; for(int i=0;i<m_Count;i++) { if(ds.Tables[0].Rows[i][columnName].ToString().Trim()!="") { ds.Tables[0].Rows[i][columnName]=ds.Tables[0].Rows[i][columnName].ToString()+postFix; } } return ds; } /// <summary> /// 获取小图对应说明 /// </summary> /// <param name="objDs"></param> /// <returns></returns> public static string GetContentStrImageS(DataSet objDs) { string retStr = ""; if( objDs!=null ) { foreach(DataRow dr in objDs.Tables[0].Select(CommonMethod.GetAppSettingPara("RowFilterSmageImg"))) { retStr+= dr["title"].ToString()+": "+dr["content"].ToString()+""; } } return retStr; } /// <summary> /// 获取配置文件参数 /// </summary> /// <param name="paraName"></param> /// <returns></returns> public static string GetAppSettingPara(string paraName) { return System.Configuration.ConfigurationSettings.AppSettings[paraName]; } /// <summary> /// 分页导航条 /// </summary> /// <param name="recordcount"></param> /// <param name="page"></param> /// <param name="pagesize"></param> /// <returns>html字符串</returns> public static string ShowPageNavi(int recordcount,int page,int pagesize) { int pagecount = 0; string tmp_str=""; if (recordcount>0) { int a= recordcount % pagesize; if(a>0) { pagecount = (recordcount / pagesize )+1; } else { pagecount = (recordcount / pagesize ); } tmp_str+= "<table width=100% cellspacing=0 cellpadding=0><tr><td height=20 class=stylePageNavi>"; tmp_str+= "符合条件记录共 "+recordcount.ToString()+" 条 当前第 "+page.ToString()+"/"+pagecount.ToString()+" 页</td><td class=stylePageNavi align=right> "; if(page<=1) { tmp_str+= "首页 上一页 "; } else { tmp_str+= "<a class=stylePageNavi href="/" mce_href="/""javascript:PageTurn(1)/">首页</a> <a class=stylePageNavi href="/" mce_href="/""javascript:PageTurn("+Convert.ToString(page-1)+")/">上一页</a> "; } if(page>=pagecount) { tmp_str+= "下一页 末页 "; } else { tmp_str+= "<a class=stylePageNavi href="/" mce_href="/""javascript:PageTurn("+Convert.ToString(page+1)+")/">下一页</a> <a class=stylePageNavi href="/" mce_href="/""javascript:PageTurn("+pagecount.ToString()+")/">末页</a> "; } tmp_str+= "跳到<select onchange=/"PageTurn(this.value)/">"; for (int i=1;i<=pagecount;i++) { if(page==i) { tmp_str+= "<option value="+i.ToString()+" selected>"+i.ToString()+"</option>"; } else { tmp_str+= "<option value="+i.ToString()+">"+i.ToString()+"</option>"; } } tmp_str+= "</select>页 "; tmp_str+= "</td></tr></table>"; } return tmp_str; } public static string ShowPageNaviByPost(int recordcount,int page,int pagesize) { int pagecount = 0; string tmp_str=""; if (recordcount>0) { int a= recordcount % pagesize; if(a>0) { pagecount = (recordcount / pagesize )+1; } else { pagecount = (recordcount / pagesize ); } tmp_str+= "<table width=100% cellspacing=0 cellpadding=0><tr><td height=20 class=stylePageNavi>"; tmp_str+= "符合条件记录共 "+recordcount.ToString()+" 条 当前第 "+page.ToString()+"/"+pagecount.ToString()+" 页</td><td class=stylePageNavi align=right> "; if(page<=1) { tmp_str+= "首页 上一页 "; } else { tmp_str+= "<a class=stylePageNavi href="/" mce_href="/""javascript:PageTurn2(1)/">首页</a> <a class=stylePageNavi href="/" mce_href="/""javascript:PageTurn2("+Convert.ToString(page-1)+")/">上一页</a> "; } if(page>=pagecount) { tmp_str+= "下一页 末页 "; } else { tmp_str+= "<a class=stylePageNavi href="/" mce_href="/""javascript:PageTurn2("+Convert.ToString(page+1)+")/">下一页</a> <a class=stylePageNavi href="/" mce_href="/""javascript:PageTurn2("+pagecount.ToString()+")/">末页</a> "; } tmp_str+= "跳到<select onchange=/"PageTurn2(this.value)/">"; for (int i=1;i<=pagecount;i++) { if(page==i) { tmp_str+= "<option value="+i.ToString()+" selected>"+i.ToString()+"</option>"; } else { tmp_str+= "<option value="+i.ToString()+">"+i.ToString()+"</option>"; } } tmp_str+= "</select>页 "; tmp_str+= "</td></tr></table>"; } return tmp_str; } /// <summary> /// 显示小图,如果没有则显示暂无图片 /// </summary> /// <param name="obj"></param> /// <returns></returns> public static string ShowImgS(Object obj) { string str_file = obj.ToString(); if(str_file=="") { str_file = "/images/missImgS.jpg"; } return str_file; } /// <summary> /// 弹出确认框 /// </summary> /// <param name="strValue">消息</param> /// <param name="urlYes">选择是的跳转页面</param> /// <param name="urlNo">选择否的跳转页面</param> public static void showConfirm(string strValue,string urlYes,string urlNo) { HttpContext ctx=HttpContext.Current ; strValue=strValue.Replace("'","").Replace(@"""","").Replace(@" ","").Replace(@" ","").Replace(@" ","").Replace("“","").Replace("”",""); strValue=strValue.Replace(@"/",@"//"); ctx.Response.Write("<mce:script language=javascript ><!-- if(window.confirm('"+strValue+"')){ window.navigate('"+urlYes+"')}else{ window.navigate('"+urlNo+"')} // --></mce:script>"); } /// <summary> /// 处理完成后弹出消息框 /// </summary> /// <param name="strValue"> 弹出的消息框的内容</param> public static void showMessageBox(string strValue) { HttpContext ctx=HttpContext.Current ; strValue=strValue.Replace("'","").Replace(@"""","").Replace(@" ","").Replace(@" ","").Replace(@" ","").Replace("“","").Replace("”",""); strValue=strValue.Replace(@"/",@"//"); ctx.Response.Write("<mce:script language=javascript ><!-- window.alert('"+strValue+"' ); // --></mce:script>"); } /// <summary> /// 处理完成后弹出消息框 /// </summary> /// <param name="strValue"> 弹出的消息框的内容</param> /// <param name="strUrl"> 跳转地址 为空则不跳转</param> public static void showMessageBox(string strValue,string strUrl) { showMessageBox(strValue); if(strUrl!="" && strUrl!=null) { HttpContext ctx=HttpContext.Current ; ctx.Response.Write("<mce:script language=javascript ><!-- window.navigate('"+strUrl+"'); // --></mce:script>"); ctx.Response.End(); } } /// <summary> /// 处理完成后弹出消息框 也可关闭窗口 /// </summary> /// <param name="strValue"> 弹出的消息框的内容</param> /// <param name="blClose">如果为TRUE 则表示弹出信息后停止该页执行,关闭窗口 否则不关闭窗口</param> public static void showMessageBox(string strValue,bool blClose) { showMessageBox(strValue); if (blClose) { HttpContext ctx=HttpContext.Current ; ctx.Response.Write("<mce:script language=javascript ><!-- window.opener = ' ';window.close(); // --></mce:script>"); ctx.Response.End(); } } /// <summary> /// 上传文件,自动大图压缩成小图 /// </summary> /// <param name="currentPage">当前页面</param> /// <param name="txtFile">大图文件名控件</param> /// <param name="imgId">小图id</param> /// <param name="filePath">上传到的服务器路径</param> /// <returns>保存的文件全名</returns> public static string UploadFileAsThumb(Page currentPage, HtmlInputFile txtFile,string imgId,string filePath) { string fileName = imgId ; //文件全名 int position = txtFile.PostedFile.FileName.LastIndexOf("."); if(position == -1) { throw new Exception("上传的文件格式不正确!"); } string extensionName = txtFile.PostedFile.FileName.Substring(position); fileName = fileName+extensionName; System.Drawing.Image Pic_L,Pic_S; System.Drawing.Image.GetThumbnailImageAbort callb=null; Pic_L= System.Drawing.Image.FromStream(txtFile.PostedFile.InputStream); if(Pic_L.Width<Pic_L.Height) { Pic_S=Pic_L.GetThumbnailImage(80,100,callb,new System.IntPtr()); } else { Pic_S=Pic_L.GetThumbnailImage(100,80,callb,new System.IntPtr()); } Pic_S.Save(currentPage.Server.MapPath(filePath+fileName),Pic_L.RawFormat); Pic_L.Dispose(); Pic_S.Dispose(); return fileName; } /// <summary> /// 上传文件 /// </summary> /// <param name="currentPage">当前页面</param> /// <param name="txtFile">文件控件</param> /// <param name="filePath">上传到的服务器路径</param> /// <param name="imageId">文件名</param> /// <returns></returns> public static string UploadFile(Page currentPage,HtmlInputFile txtFile,string filePath,string imageId) { if(txtFile.PostedFile.FileName.Length ==0) return string.Empty ; string fileName = imageId ; int position = txtFile.PostedFile.FileName.LastIndexOf("."); if(position == -1) { throw new Exception("上传的文件格式不正确!"); } string extensionName = txtFile.PostedFile.FileName.Substring(position); fileName = fileName+extensionName; txtFile.PostedFile .SaveAs(currentPage.Server.MapPath( filePath)+ fileName) ; return fileName; } /// <summary> /// 楼盘向上翻滚的页面效果 /// </summary> /// <param name="controlId">控件名称(同一个页面用多个,必须用不一样的id值)</param> /// <param name="arrStroll">滚动文字数组,不可有单引号</param> /// <returns>js的脚本,写在需要的地方</returns> public static string UpStrollInfo(string controlId,Array arrStroll) { if(controlId.Length==0) return ""; if(arrStroll.Length==0) return ""; string strRet = ""; strRet+="<mce:script type="text/javascript"><!--</div> <div>"; strRet+="var marqueeContent"+controlId+"=new Array(); "; for(int i=0;i<arrStroll.Length;i++) { strRet+=" marqueeContent"+controlId+"["+i.ToString()+"]='"+arrStroll.GetValue(i).ToString()+"' "; } strRet+="var marqueeInterval"+controlId+"=new Array(); ";//定义一些常用而且要经常用到的变量 strRet+="var marqueeId"+controlId+"=0; "; strRet+="var marqueeDelay"+controlId+"=2000; "; strRet+="var marqueeHeight"+controlId+"=20; "; //接下来的是定义一些要使用到的函数 strRet+="function initMarquee"+controlId+"() { "; strRet+="var str=marqueeContent"+controlId+"[0]; "; strRet+="document.write('<div id=marqueeBox"+controlId+" style="/" mce_style="/""overflow:hidden;height:'+marqueeHeight"+controlId+"+'px/" "; strRet+=" onmouseover=/"clearInterval(marqueeInterval"+controlId+"[0])/" "; strRet+=" onmouseout=/"marqueeInterval"+controlId+"[0]=setInterval(//'startMarquee"+controlId+"()//',marqueeDelay"+controlId+")/"><div>'+str+'</div></div>'); "; strRet+="marqueeId"+controlId+"++; "; strRet+="marqueeInterval"+controlId+"[0]=setInterval(/"startMarquee"+controlId+"()/",marqueeDelay"+controlId+"); "; strRet+="} "; strRet+="function startMarquee"+controlId+"() { "; strRet+="var str=marqueeContent"+controlId+"[marqueeId"+controlId+"]; "; strRet+="marqueeId"+controlId+"++; "; strRet+="if(marqueeId"+controlId+">=marqueeContent"+controlId+".length) marqueeId"+controlId+"=0; "; strRet+="if(marqueeBox"+controlId+".childNodes.length==1) { "; strRet+="var nextLine=document.createElement('DIV'); "; strRet+="nextLine.innerHTML=str; "; strRet+="marqueeBox"+controlId+".appendChild(nextLine); "; strRet+="} "; strRet+="else { "; strRet+="marqueeBox"+controlId+".childNodes[0].innerHTML=str; "; strRet+="marqueeBox"+controlId+".appendChild(marqueeBox"+controlId+".childNodes[0]); "; strRet+="marqueeBox"+controlId+".scrollTop=0; "; strRet+="} "; strRet+="clearInterval(marqueeInterval"+controlId+"[1]); "; strRet+="marqueeInterval"+controlId+"[1]=setInterval(/"scrollMarquee"+controlId+"()/",20); "; strRet+="} "; strRet+="function scrollMarquee"+controlId+"() { "; strRet+="marqueeBox"+controlId+".scrollTop++; "; strRet+="if(marqueeBox"+controlId+".scrollTop%marqueeHeight"+controlId+"==(marqueeHeight"+controlId+"-1)){ "; strRet+="clearInterval(marqueeInterval"+controlId+"[1]); "; strRet+="} "; strRet+="} "; strRet+="initMarquee"+controlId+"(); "; strRet+=" // --></mce:script> "; return strRet; } /// <summary> /// 取得部分rows /// </summary> /// <param name="dt"></param> /// <param name="rowStart"></param> /// <param name="rowCount"></param> /// <returns></returns> public static DataTable GetTablePart(DataTable dt,int rowStart,int rowCount) { DataTable dtRet = dt.Copy(); int i=0; int j=dtRet.Rows.Count; while(i<rowStart) { dtRet.Rows.Remove(dtRet.Rows[0]); i++; } while(dtRet.Rows.Count >rowCount) dtRet.Rows.RemoveAt(rowCount); return dtRet; } /// <summary> /// 把数字1显示为"有" /// </summary> /// <param name="lblShow"></param> public static void ConvertNum1ToTxt(System.Web.UI.HtmlControls.HtmlGenericControl lblShow) { if(lblShow.InnerHtml==null || lblShow.InnerHtml=="" || lblShow.InnerHtml=="0") { lblShow.InnerHtml = "无"; } else { lblShow.InnerHtml = "有"; } } /// <summary> /// 把数字1显示为"有" /// </summary> /// <param name="lblShow"></param> public static void ConvertNum1ToTxt(Label lbl) { if(lbl.Text==null || lbl.Text=="" || lbl.Text=="0") { lbl.Text = "无"; } else { lbl.Text = "有"; } } /// <summary> /// 有关去除字符串中的HTML标识 /// </summary> /// <param name="Content">要处理的字符串</param> /// public static string RemoveHTML(object obj) { string Content = ToStr(obj); if(Content.Length>0) { int htmlFlag1=Content.IndexOf("<"); int htmlFlag2=Content.IndexOf(">"); while(htmlFlag1>=0 && htmlFlag2>=0 && htmlFlag2>htmlFlag1) { Content=Content.Remove(htmlFlag1,htmlFlag2-htmlFlag1+1); htmlFlag1=Content.IndexOf("<"); htmlFlag2=Content.IndexOf(">"); } } return Content; } /// <summary> /// 除字符串中的零 /// </summary> /// <param name="Content"></param> /// <returns></returns> public static string RemoveZero(Object content) { return content.ToString().TrimEnd('0').TrimEnd('.'); } public static bool SetZeroValue(DataTable dt) { if(dt==null) { return false; } else { for(int i=0;i<dt.Rows.Count;i++) for(int j=0;j<dt.Columns.Count;j++) { System.Type datatype = dt.Columns[j].DataType; if( datatype == System.Type.GetType("System.Int16")|| datatype == System.Type.GetType("System.Int32")|| datatype == System.Type.GetType("System.Int64")|| datatype == System.Type.GetType("System.UInt16")|| datatype == System.Type.GetType("System.UInt32")|| datatype == System.Type.GetType("System.UInt64")|| datatype == System.Type.GetType("System.Byte")|| datatype == System.Type.GetType("System.Decimal")|| datatype == System.Type.GetType("System.Single")|| datatype == System.Type.GetType("System.Double") ) { dt.Rows[i][j] = 0; } else if(datatype == System.Type.GetType("System.DateTime")) { dt.Rows[i][j] = System.DateTime.Now; } else if(datatype == System.Type.GetType("System.Boolean")) { dt.Rows[i][j] = false; } else if(datatype == System.Type.GetType("System.Guid")) { dt.Rows[i][j] = Guid.Empty; } else if(datatype == System.Type.GetType("System.String")) { } else { dt.Rows[i][j] = 0; } } return true; } } public static void RemoveDecimalZero(DataSet ds) { for(int i=0;i<ds.Tables[0].Rows.Count;i++) { for(int j=0;j<ds.Tables[0].Columns.Count;j++) { if(ds.Tables[0].Columns[j].DataType.ToString () == "System.Decimal") { if(ds.Tables[0].Rows[i][j].ToString().IndexOf('.')>0) { ds.Tables[0].Rows[i][j]=ds.Tables[0].Rows[i][j].ToString().TrimEnd('0').TrimEnd('.'); } } } } } /// <summary> /// Persist ScrollBar's Position After a PostBack /// 记录住滚动条位置,保持页面位置不变 /// </summary> public static void PersistScrollPosition(System.Web.UI.Page page) { System.Text.StringBuilder saveScrollPosition = new System.Text.StringBuilder(); System.Text.StringBuilder setScrollPosition = new System.Text.StringBuilder(); page.RegisterHiddenField("__SCROLLPOS", "0"); saveScrollPosition.Append("<mce:script language='javascript'><!-- "); saveScrollPosition.Append("function saveScrollPosition() {"); saveScrollPosition.Append(" document.forms[0].__SCROLLPOS.value = document.body.scrollTop;"); //电子地图游戏广告 saveScrollPosition.Append("try{TO_TOP();}catch(e){}"); saveScrollPosition.Append("}"); saveScrollPosition.Append("document.body.onscroll=saveScrollPosition;"); saveScrollPosition.Append(" // --></mce:script>"); page.RegisterStartupScript("saveScroll", saveScrollPosition.ToString()); if (page.IsPostBack) { setScrollPosition.Append("<mce:script language='javascript'><!-- "); setScrollPosition.Append("function setScrollPosition() {"); setScrollPosition.Append(" document.body.scrollTop = " + page.Request["__SCROLLPOS"] + ";"); setScrollPosition.Append("}"); setScrollPosition.Append("document.body.onload=setScrollPosition;"); setScrollPosition.Append(" // --></mce:script>"); page.RegisterStartupScript("setScroll", setScrollPosition.ToString()); } } /// <summary> /// 发送EMail-Html格式 /// </summary> /// <param name="form"></param> /// <param name="to"></param> /// <param name="subject"></param> /// <param name="body"></param> /// <returns></returns> public static bool sendMailHtml(string to,string subject,string body) { try { string host = ConfigurationSettings.AppSettings.Get("MailHost"); string name = ConfigurationSettings.AppSettings.Get("MailUserName"); string pass = ConfigurationSettings.AppSettings.Get("MailUserPass"); string port = ConfigurationSettings.AppSettings.Get("MailPort"); string MailForm = ConfigurationSettings.AppSettings.Get("MailForm"); OpenSmtp.Mail.Smtp smtp; if(port!="" && port!=null) { smtp =new OpenSmtp.Mail.Smtp(host,name,pass,Convert.ToInt32(port)); } else { smtp =new OpenSmtp.Mail.Smtp(host,name,pass); } OpenSmtp.Mail.MailMessage mess = new OpenSmtp.Mail.MailMessage(MailForm,to); mess.Subject = subject; mess.HtmlBody = body ; mess.Charset = "gb2312"; smtp.SendTimeout=50000; smtp.SendMail(mess); return true; } catch { return false; } } /// <summary> /// 发送EMail-Html格式 /// </summary> /// <param name="form"></param> /// <param name="to"></param> /// <param name="subject"></param> /// <param name="body"></param> /// <returns></returns> public static bool sendMailText(string to,string subject,string body) { try { string host = ConfigurationSettings.AppSettings.Get("MailHost"); string name = ConfigurationSettings.AppSettings.Get("MailUserName"); string pass = ConfigurationSettings.AppSettings.Get("MailUserPass"); string port = ConfigurationSettings.AppSettings.Get("MailPort"); string MailForm = ConfigurationSettings.AppSettings.Get("MailForm"); OpenSmtp.Mail.Smtp smtp; if(port!="" && port!=null) { smtp =new OpenSmtp.Mail.Smtp(host,name,pass,Convert.ToInt32(port)); } else { smtp =new OpenSmtp.Mail.Smtp(host,name,pass); } OpenSmtp.Mail.MailMessage mess = new OpenSmtp.Mail.MailMessage(MailForm,to); mess.Subject = subject; //mess.HtmlBody = "<html><body>" + body.Replace(" ","").Replace(" "," ") + "</body></html>"; mess.Body = body; mess.Charset = "gb2312"; smtp.SendTimeout=50000; smtp.SendMail(mess); return true; } catch { return false; } } /// <summary> /// 获取request中的参数值 /// </summary> /// <param name="page"></param> /// <param name="name"></param> /// <returns></returns> public static string GetRequest(System.Web.UI.Page page,string name) { string returnValue=""; if(page.Request[name]!=null) { returnValue=page.Request[name].ToString().Trim(); } return returnValue; } public static string GetInformationTypeById(string id) { string sql=" select * from d_informationtype where InformationTypeId='"+id+"' "; int RecordCount=0; DataSet ds=ServiceInterfaces.CommonService.GetDataPage(sql,1,1,out RecordCount); if(RecordCount==1) { return (ds.Tables[0].Rows[0]["Category"].ToString()); } else { return ""; } } public static string GetInformationTypeByCategory(string Category) { string sql=" select * from d_informationtype where Category='"+Category+"' "; int RecordCount=0; DataSet ds=ServiceInterfaces.CommonService.GetDataPage(sql,1,1,out RecordCount); if(RecordCount==1) { return (ds.Tables[0].Rows[0]["InformationTypeId"].ToString()); } else { return ""; } } /// <summary> /// 将数据字典取出放到cache里面.System.Web.HttpContext.Current.Cache是所有用户共享的 /// </summary> /// <returns></returns> public static DataSet GetInformationType() { if(System.Web.HttpContext.Current.Cache["d_InformationType"]!=null) { return (DataSet)System.Web.HttpContext.Current.Cache["d_InformationType"]; } else { DataSet ds=ServiceInterfaces.CommonService.GetLookUpTable("InformationType"); double interval=120; if(System.Configuration.ConfigurationSettings.AppSettings["Dictionary.interval"]!=null) { interval=System.Double.Parse(System.Configuration.ConfigurationSettings.AppSettings["Dictionary.interval"]); } System.Web.HttpContext.Current.Cache.Insert("d_InformationType",ds,null,System.Web.Caching.Cache.NoAbsoluteExpiration,TimeSpan.FromMinutes(interval));//120分钟更新一次 return (DataSet)System.Web.HttpContext.Current.Cache["d_InformationType"]; } } /// <summary> /// 根据条件取得字典 /// </summary> /// <param name="condition"></param> /// <returns></returns> public static DataTable GetInformationType(string condition) { DataSet ds=Biz.CommonMethod.GetInformationType(); if(condition.Length>0) { DataRow[] drs=ds.Tables[0].Select(condition); DataTable dt=new DataTable(); dt.Columns.Add(new DataColumn("InformationTypeId",System.Type.GetType("System.String"))); dt.Columns.Add(new DataColumn("Category",System.Type.GetType("System.String"))); dt.Columns.Add(new DataColumn("ParentId",System.Type.GetType("System.String"))); dt.Columns.Add(new DataColumn("GroupType",System.Type.GetType("System.String"))); for(int i=0;i<drs.Length;i++) { DataRow row=dt.NewRow(); row["InformationTypeId"]=drs[i]["InformationTypeId"].ToString(); row["Category"]=drs[i]["Category"].ToString(); row["ParentId"]=drs[i]["ParentId"].ToString(); row["GroupType"]=drs[i]["GroupType"].ToString(); dt.Rows.Add(row); } return dt; } else { return ds.Tables[0]; } } public static string Replace(Object source,string oldValue,string newValue) { return source.ToString().Replace(oldValue,newValue); } } } </div>
试试其它关键字
前台常用方法
同语言下
.
gzip压缩
.
实现http多线程断点续传下载文件
.
实现多线程断点续传下载大文件
.
生成字符串的 CheckSum
.
根据 UserAgent 获取浏览器的类型和版本
.
根据 Agent 判断是否是智能手机
.
隐藏手机号中间四位为*方法
.
合并图片(二维码和其他图片合并)
.
ASP.NET CORE中判断是否移动端打开网页
.
ASP.NET(C#)实现页面计时(定时)自动跳转
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
wd63575706
贡献的其它代码
(
1
)
.
asp.net前台常用方法
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3