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.Text;
using System.Text.RegularExpressions;
/// <summary>
/// WebHelper 的摘要描述
/// </summary>
public class WebHelper
{
public WebHelper()
{
//
// TODO: 在此加入建構函式的程式碼
//
}
#region double類型的檢驗
/// <summary>
/// double類型的檢驗
/// </summary>
/// <param name="str">str</param>
/// <returns></returns>
public static bool Numcheck(string str)
{
if (str.Trim() == "")
{
return false;
}
else
{
return Regex.IsMatch(str, @"^[+-]?\d*[.]?\d*$");
}
}
public static bool Intcheck(string str)
{
if (str.Trim() == "")
{
return false;
}
else
{
return Regex.IsMatch(str, @"^[+-]?\d*$");
}
}
#endregion
#region 顯示提示框 flase為可視
/// <summary>
/// 顯示提示框 flase為可視
/// </summary>
/// <param name="msg">字符串</param>
/// <param name="isInHead">flase為可視</param>
public static void JsAlert(string msg, bool isInHead)
{
string m = string.Format("alert(\"{0}\");", msg.Replace(@"\", @"\\").Replace("\"", "\\\"").Replace("\n", @"\n"));
ExecuteJs(m, isInHead);
}
#endregion
#region 執行JS
public static void ExecuteJs(string script, bool isInHead)
{
string str = string.Format("\n<script type=\"text/javascript\">\n{0}\n</script>\n", script);
if (!isInHead)
{
((Page)HttpContext.Current.Handler).ClientScript.RegisterStartupScript(Type.GetType("System.String"), Guid.NewGuid().ToString(), str);
}
else
{
HtmlGenericControl child = new HtmlGenericControl("script");
child.Attributes.Add("type", "text/javascript");
child.InnerHtml = "\n" + script + "\n";
Page handler = (Page)HttpContext.Current.Handler;
handler.Header.Controls.Add(child);
}
}
#endregion
#region 字符格式處理
//解碼JS過濾危險字符函數
public string strHTMLDecode(string str)
{
return str.Replace("&", "&").Replace(">", ">").Replace("<", "<").Trim();
}
/// <summary>
/// 去掉所有的特殊字符
/// </summary>
/// <param name="tt"></param>
/// <returns></returns>
public string strReplacenoall(string tt)
{
tt = tt.Replace( "'", "");
tt = tt.Replace("&", "");
tt = tt.Replace("<", "");
tt = tt.Replace(">", "");
tt = tt.Replace("@", "");
return tt;
}
//處理字符格式
public string strFormatEncode(string str)
{
str = str.Trim().Replace("'", "''");//特殊字符處理:空格 和 單引號,在SQL语法中将连续两个单引号"''"视为一个单引号"'"
//處理換行,在每個新行的前面添加兩個全角空格
//****************
Regex r = new Regex(@"(\r\n(( )| )+)(?<正文> +)", RegexOptions.IgnoreCase);
Match m = null;
for (m = r.Match(str); m.Success; m = m.NextMatch())
{
str = str.Replace(m.Groups[0].ToString(), "<br />" + m.Groups["正文"].ToString());
}
//****************
str = str.Replace("\r\n", "<br />");
return str;
}
//處理字符格式
public string strFormatDecode(string str)
{
str = str.Replace("<br />", "\r\n");
return str;
}
#endregion
#region 合并欄位
/// <summary>
/// 合并欄位
/// </summary>
/// <param name="vTip"></param>
/// <param name="vHeight"></param>
/// <param name="vWidth"></param>
/// <returns></returns>
public static void VSort(GridView gdv, string str_label, int begincol, int endcol)
{
int i;
int j;
int intSpan, NowSpan = 0;
string strTemp;
for (i = 0; i <= gdv.Rows.Count - 1; )
{
intSpan = 1;
Label l_bill_no = (Label)gdv.Rows[i].FindControl(str_label);
strTemp = l_bill_no.Text;
for (j = i + 1; j <= gdv.Rows.Count - 1; j++)
{
string str_row = "";
Label l_bill_no1 = (Label)gdv.Rows[j].FindControl(str_label);
str_row = l_bill_no1.Text;
if (strTemp == str_row)
{
intSpan++;
for (int k = begincol; k <= endcol; k++)
{
gdv.Rows[i].Cells[k].RowSpan = intSpan;
gdv.Rows[j].Cells[k].Visible = false;
}
}
else
{
break;
}
}
NowSpan += intSpan;
i = NowSpan - 1;
}
}
public static void VSortHylink(GridView gdv, string str_label, int begincol, int endcol)
{
int i;
int j;
int intSpan, NowSpan = 0;
string strTemp;
for (i = 0; i <= gdv.Rows.Count - 1; )
{
intSpan = 1;
HyperLink l_bill_no = (HyperLink)gdv.Rows[i].FindControl(str_label);
strTemp = l_bill_no.Text;
for (j = i + 1; j <= gdv.Rows.Count - 1; j++)
{
string str_row = "";
HyperLink l_bill_no1 = (HyperLink)gdv.Rows[j].FindControl(str_label);
str_row = l_bill_no1.Text;
if (strTemp == str_row)
{
intSpan++;
for (int k = begincol; k <= endcol; k++)
{
gdv.Rows[i].Cells[k].RowSpan = intSpan;
gdv.Rows[j].Cells[k].Visible = false;
}
}
else
{
break;
}
}
NowSpan += intSpan;
i = NowSpan - 1;
}
}
#endregion
}