using System; using System.Collections; using System.Collections.Generic; using System.Text; using System.IO; using System.Security; using System.Security.Cryptography; using System.Resources; namespace WebBase { /// /// The web helper class contains shared functions used by page and user controls, as base class of Page and UserControl. /// public class WebHelper { #region Helper Methods /// /// Gets the string param. /// /// The request. /// Name of the param. /// The error return. /// The param value. public static string GetStringParam(System.Web.HttpRequest request, string paramName, string errorReturn) { string retStr = request.Form[paramName]; if (retStr == null) { retStr = request.QueryString[paramName]; } if (retStr == null) { return errorReturn; } return retStr; } /// /// Gets the int param. /// /// The request. /// Name of the param. /// The error return. /// The param value. public static int GetIntParam(System.Web.HttpRequest request, string paramName, int errorReturn) { string retStr = request.Form[paramName]; if (retStr == null) { retStr = request.QueryString[paramName]; } if (retStr == null || retStr.Trim() == string.Empty) { return errorReturn; } try { return Convert.ToInt32(retStr); } catch { return errorReturn; } } /// /// Gets the date time param. /// /// The request. /// Name of the param. /// The error return. /// The param value. public static DateTime GetDateTimeParam(System.Web.HttpRequest request, string paramName, DateTime errorReturn) { string retStr = request.Form[paramName]; if (retStr == null) { retStr = request.QueryString[paramName]; } if (retStr == null || retStr.Trim() == string.Empty) { return errorReturn; } try { return Convert.ToDateTime(retStr); } catch { return errorReturn; } } /// /// Strongs the typed. /// /// The obj. /// The strong typed instance. public static ObjectType StrongTyped(object obj) { return (ObjectType)obj; } /// /// Toes the js single quote safe string. /// /// The STR. /// The formated str. public static string ToJsSingleQuoteSafeString(string str) { return str.Replace("'", "\\'"); } /// /// Toes the js double quote safe string. /// /// The STR. /// The formated str. public static string ToJsDoubleQuoteSafeString(string str) { return str.Replace("\"", "\\\""); } /// /// Toes the VBS quote safe string. /// /// The STR. /// The formated str. public static string ToVbsQuoteSafeString(string str) { return str.Replace("\"", "\"\""); } /// /// Toes the SQL quote safe string. /// /// The STR. /// The formated str. public static string ToSqlQuoteSafeString(string str) { return str.Replace("'", "''"); } /// /// Texts to HTML. /// /// The TXT STR. /// The formated str. public static string TextToHtml(string txtStr) { return txtStr.Replace(" ", " ").Replace("\t", "    "). Replace("<", "<").Replace(">", ">").Replace("\r", "").Replace("\n", "
"); } #endregion #region Resource private static Dictionary stringResources = new Dictionary(); private static System.Globalization.CultureInfo defaultCulture = null; /// /// Gets or sets the default culture. /// /// The default culture. public static System.Globalization.CultureInfo DefaultCulture { get { return defaultCulture ?? System.Threading.Thread.CurrentThread.CurrentUICulture; } set { defaultCulture = value; } } /// /// Loads the resources. /// /// Name of the resource. /// The ci. public static void LoadResources(string resourceName, System.Globalization.CultureInfo ci) { string resFileName = System.Web.HttpRuntime.BinDirectory + resourceName + "." + ci.ToString() + ".resources"; if (System.IO.File.Exists(resFileName)) { lock (stringResources) { if (!stringResources.ContainsKey(ci.ToString())) { stringResources.Add(ci.ToString(), new Hashtable()); try { ResourceReader reader = new ResourceReader(resFileName); IDictionaryEnumerator en = reader.GetEnumerator(); while (en.MoveNext()) { stringResources[ci.ToString()].Add(en.Key, en.Value); } reader.Close(); } catch { } } } } } /// /// Loads the resources. /// /// Name of the resource. public static void LoadResources(string resourceName) { LoadResources(resourceName, DefaultCulture); } /// /// Gets the string. /// /// The key. /// The resouce value. public static string GetString(string key) { return GetString(key, WebHelper.DefaultCulture); } /// /// Gets the string. /// /// The key. /// The ci. /// The resouce value. public static string GetString(string key, System.Globalization.CultureInfo ci) { if (stringResources.ContainsKey(ci.ToString())) { if (stringResources[ci.ToString()].Contains(key)) { return stringResources[ci.ToString()][key].ToString(); } } return string.Empty; } #endregion #region ClientScriptFactoryHelper /// /// Common Client Script /// public class ClientScriptFactoryHelper { #region Constructors /// /// Initializes a new instance of the class. /// public ClientScriptFactoryHelper() { } #endregion /// /// Wraps the script tag. /// /// The scripts. /// The script. public string WrapScriptTag(params string[] scripts) { if (scripts != null && scripts.Length > 0) { StringBuilder sb = new StringBuilder(); sb.Append("\r\n\r\n"); return sb.ToString(); } else { return string.Empty; } } /// /// Pops the alert. /// /// The MSG. /// The script. public string PopAlert(string msg) { return string.Format(" window.alert('{0}'); ", ToJsSingleQuoteSafeString(msg)); } /// /// Pops the confirm. /// /// The MSG. /// The script. public string PopConfirm(string msg) { return string.Format(" window.confirm('{0}') ", ToJsSingleQuoteSafeString(msg)); } /// /// Pops the prompt. /// /// The MSG. /// The default value. /// The script. public string PopPrompt(string msg, string defaultValue) { return string.Format(" window.prompt('{0}', '{1}') ", ToJsSingleQuoteSafeString(msg), ToJsSingleQuoteSafeString(defaultValue)); } /// /// Closes the self. /// /// The script. public string CloseSelf() { return " window.close(); "; } /// /// Closes the parent. /// /// The script. public string CloseParent() { return " if (window.parent) { window.parent.close(); } "; } /// /// Closes the opener. /// /// The script. public string CloseOpener() { return " if (window.opener) { window.opener.close(); } "; } /// /// Refreshes the self. /// /// The script. public string RefreshSelf() { return " window.location += ' '; "; } /// /// Refreshes the opener. /// /// The script. public string RefreshOpener() { return " if (window.opener) { window.opener.location += ' '; } "; } /// /// Refreshes the parent. /// /// The script. public string RefreshParent() { return " if (window.parent) { window.parent.location += ' '; } "; } /// /// Shows the modal dialog. /// /// The URL. /// if set to true [status]. /// if set to true [resizable]. /// The height. /// The width. /// The top. /// The left. /// if set to true [scroll]. /// The script. public string ShowModalDialog(string url, bool status, bool resizable, int height, int width, int top, int left, bool scroll) { return string.Format(" window.showModalDialog('{0}', window, 'status={1},resizable={2},dialogHeight={3}px,dialogWidth={4}px,dialogTop={5},dialogLeft={6},scroll={7},unadorne=yes'); ", ToJsSingleQuoteSafeString(url), (status ? 1 : 0), (resizable ? 1 : 0), height, width, top, left, (scroll ? 1 : 0)); } /// /// Shows the modal dialog. /// /// The URL. /// if set to true [status]. /// if set to true [resizable]. /// The height. /// The width. /// if set to true [center]. /// if set to true [scroll]. /// The script. public string ShowModalDialog(string url, bool status, bool resizable, int height, int width, bool center, bool scroll) { return string.Format(" window.showModalDialog('{0}', window, 'status={1},resizable={2},dialogHeight={3}px,dialogWidth={4}px,center={5},scroll={6},unadorne=yes'); ", ToJsSingleQuoteSafeString(url), (status ? 1 : 0), (resizable ? 1 : 0), height, width, (center ? 1 : 0), (scroll ? 1 : 0)); } /// /// Shows the modeless dialog. /// /// The URL. /// if set to true [status]. /// if set to true [resizable]. /// The height. /// The width. /// The top. /// The left. /// if set to true [scroll]. /// The script. public string ShowModelessDialog(string url, bool status, bool resizable, int height, int width, int top, int left, bool scroll) { return string.Format(" window.showModelessDialog('{0}', window, 'status={1},resizable={2},dialogHeight={3}px,dialogWidth={4}px,dialogTop={5},dialogLeft={6},scroll={7},unadorne=yes'); ", ToJsSingleQuoteSafeString(url), (status ? 1 : 0), (resizable ? 1 : 0), height, width, top, left, (scroll ? 1 : 0)); } /// /// Shows the modeless dialog. /// /// The URL. /// if set to true [status]. /// if set to true [resizable]. /// The height. /// The width. /// if set to true [center]. /// if set to true [scroll]. /// The script. public string ShowModelessDialog(string url, bool status, bool resizable, int height, int width, bool center, bool scroll) { return string.Format(" window.showModelessDialog('{0}', window, 'status={1},resizable={2},dialogHeight={3}px,dialogWidth={4}px,center={5},scroll={6},unadorne=yes'); ", ToJsSingleQuoteSafeString(url), (status ? 1 : 0), (resizable ? 1 : 0), height, width, (center ? 1 : 0), (scroll ? 1 : 0)); } /// /// Selfs the go back. /// /// The script. public string SelfGoBack() { return " window.history.back(); "; } /// /// Parents the go back. /// /// The script. public string ParentGoBack() { return " if (window.parent) { window.parent.history.back(); } "; } /// /// Openers the go back. /// /// The script. public string OpenerGoBack() { return " if (window.opener) { window.opener.history.back(); } "; } /// /// Opens the specified URL. /// /// The URL. /// Name of the frame. /// if set to true [status]. /// if set to true [location]. /// if set to true [menubar]. /// if set to true [resizable]. /// The height. /// The width. /// The top. /// The left. /// if set to true [scrollbars]. /// if set to true [toolbar]. /// The script. public string Open(string url, string frameName, bool status, bool location, bool menubar, bool resizable, int height, int width, int top, int left, bool scrollbars, bool toolbar) { return string.Format(" window.open('{0}', '{1}', 'status={2},location={3},menubar={4},resizable={5},height={6}px,width={7}px,top={8},left={9},scrollbars={10},toolbar={11}'); ", ToJsSingleQuoteSafeString(url), ToJsSingleQuoteSafeString(frameName), (status ? 1 : 0), (location ? 1 : 0), (menubar ? 1 : 0), (resizable ? 1 : 0), height, width, top, left, (scrollbars ? 1 : 0), (toolbar ? 1 : 0)); } /// /// Opens the specified URL. /// /// The URL. /// Name of the frame. /// The script. public string Open(string url, string frameName) { return string.Format(" window.open('{0}', '{1}'); ", ToJsSingleQuoteSafeString(url), ToJsSingleQuoteSafeString(frameName)); } /// /// Calls the client validator. /// /// The prefix. /// The validators. /// The script. protected string CallClientValidator(string prefix, params System.Web.UI.WebControls.BaseValidator[] validators) { if (validators != null && validators.Length > 0) { StringBuilder sb = new StringBuilder(); foreach (System.Web.UI.WebControls.BaseValidator validator in validators) { sb.Append(string.Format(" ValidatorValidate({1}{0}); ", validator.ID, prefix)); } return sb.ToString(); } else { return string.Empty; } } /// /// Toes the js string array. /// /// The STRS. /// The script. public string ToJsStringArray(params string[] strs) { if (strs != null && strs.Length > 0) { StringBuilder sb = new StringBuilder(); sb.Append(" new Array("); foreach (string str in strs) { sb.Append(string.Format("'{0}', ", str.Replace("'", "\\'"))); } return sb.ToString().TrimEnd(',', ' ') + ");"; } else { return " new Array;"; } } } #endregion } }