//******************************************************************************************************
// ClientSideExtensions.cs - Gbtc
//
// Copyright © 2012, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the Eclipse Public License -v 1.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain a copy of the License at:
//
// http://www.opensource.org/licenses/eclipse-1.0.php
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 12/22/2010 - Pinal C. Patel
// Generated original version of source code.
// 06/15/2011 - Pinal C. Patel
// Added Refresh(), Show(), ShowDialog(), ShowPopup(), Close() and MsgBox() client-side extensions.
// 12/20/2012 - Starlynn Danyelle Gilliam
// Modified Header.
//
//******************************************************************************************************
using System;
using System.Text;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
namespace GSF.Web.UI
{
#region [ Enumerations ]
///
/// Specifies the buttons to be displayed in a message box.
///
[Flags]
public enum MsgBoxStyle
{
///
/// OK Button.
///
OKOnly = 0,
///
/// OK and Cancel buttons.
///
OKCancel = 1,
///
/// Abort,Retry, and Ignore buttons.
///
AbortRetryIgnore = 2,
///
/// Yes, No, and Cancel buttons.
///
YesNoCancel = 3,
///
/// Yes and No buttons.
///
YesNo = 4,
///
/// Retry and Cancel buttons.
///
RetryCancel = 5,
///
/// Critical message.
///
Critical = 16,
///
/// Warning query.
///
Question = 32,
///
/// Warning message.
///
Exclamation = 48,
///
/// Information message.
///
Information = 64,
///
/// First button is default.
///
DefaultButton1 = 0,
///
/// Second button is default.
///
DefaultButton2 = 256,
///
/// Third button is default.
///
DefaultButton3 = 512,
///
/// Fourth button is default.
///
DefaultButton4 = 768,
///
/// Application modal message box.
///
ApplicationModal = 0,
///
/// System modal message box.
///
SystemModal = 4096,
}
#endregion
///
/// Defines javascript client-side extensions for web controls.
///
public static class ClientSideExtensions
{
///
/// Performs JavaScript encoding on given string.
///
/// The string to be encoded.
public static string JavaScriptEncode(ref string text)
{
text = text.Replace("\\", "\\\\");
text = text.Replace("'", "\\'");
text = text.Replace("\"", "\\\"");
text = text.Replace(Convert.ToChar(8), '\b');
text = text.Replace(Convert.ToChar(9), '\t');
text = text.Replace(Convert.ToChar(10), '\r');
text = text.Replace(Convert.ToChar(12), '\f');
text = text.Replace(Convert.ToChar(13), '\n');
return text;
}
///
/// Decodes JavaScript characters from given string.
///
/// The string to be decoded.
public static string JavaScriptDecode(ref string text)
{
text = text.Replace("\\\\", "\\");
text = text.Replace("\\'", "'");
text = text.Replace("\\\"", "\"");
text = text.Replace('\b', Convert.ToChar(8));
text = text.Replace('\t', Convert.ToChar(9));
text = text.Replace('\r', Convert.ToChar(10));
text = text.Replace('\f', Convert.ToChar(12));
text = text.Replace('\n', Convert.ToChar(13));
return text;
}
///
/// Registers a default button to be activated when the ENTER key is pressed inside this .
///
/// for which the default button is to be registered.
/// Button to be assigned as the default action handler.
public static void SetDefaultButton(this TextBox textbox, Control control)
{
if (!textbox.Page.ClientScript.IsClientScriptBlockRegistered("DefaultButton"))
{
StringBuilder script = new StringBuilder();
script.Append("\r\n");
textbox.Page.ClientScript.RegisterClientScriptBlock(textbox.Page.GetType(), "DefaultButton", script.ToString());
}
textbox.Attributes.Add("OnKeyDown", "javascript:DefaultButton(event, \'" + control.ClientID + "\')");
}
///
/// Registers the to be disabled after one click to prevent multiple postbacks.
///
/// for which submit once script is to be registered.
public static void SetSubmitOnce(this Button button)
{
StringBuilder script = new StringBuilder();
if (button.CausesValidation)
{
script.Append("if (typeof(Page_ClientValidate) == \'function\') {");
script.Append("if (Page_ClientValidate() == false) { return false; }}");
}
script.Append("this.disabled = true;");
script.AppendFormat("{0};", button.Page.ClientScript.GetPostBackEventReference(button, null));
button.OnClientClick = script.ToString();
}
///
/// Brings the 's browser window to the foreground.
///
/// to be brought to the foreground.
public static void BringToFront(this Page page)
{
if (!page.ClientScript.IsStartupScriptRegistered("BringToFront"))
{
StringBuilder script = new StringBuilder();
script.Append("\r\n");
page.ClientScript.RegisterStartupScript(page.GetType(), "BringToFront", script.ToString());
}
}
///
/// Pushes the 's browser window to the background.
///
/// to be pushed to the background.
public static void PushToBack(this Page page)
{
if (!page.ClientScript.IsStartupScriptRegistered("PushToBack"))
{
StringBuilder script = new StringBuilder();
script.Append("\r\n");
page.ClientScript.RegisterStartupScript(page.GetType(), "PushToBack", script.ToString());
}
}
///
/// Registers this to perform a page refresh.
///
/// to refresh.
/// Refreshes the after the page content has loaded.
public static void Refresh(this Page page, bool refreshOnLoad = false)
{
if (!page.ClientScript.IsClientScriptBlockRegistered("Refresh"))
page.ClientScript.RegisterClientScriptBlock(page.GetType(), "Refresh", GetRefreshScript());
if (refreshOnLoad)
{
// Refresh page after page load.
if (!page.ClientScript.IsStartupScriptRegistered("RefreshPostLoad"))
{
StringBuilder script = new StringBuilder();
script.Append("\r\n");
page.ClientScript.RegisterStartupScript(page.GetType(), "RefreshPostLoad", script.ToString());
}
}
else
{
// Refresh page before page load.
if (!page.ClientScript.IsClientScriptBlockRegistered("RefreshPreLoad"))
{
StringBuilder script = new StringBuilder();
script.Append("\r\n");
page.ClientScript.RegisterClientScriptBlock(page.GetType(), "RefreshPreLoad", script.ToString());
}
}
}
///
/// Registers the to perform a page refresh.
///
/// that will perform a page refresh when clicked.
public static void Refresh(this Control control)
{
if (!control.Page.ClientScript.IsClientScriptBlockRegistered("Refresh"))
control.Page.ClientScript.RegisterClientScriptBlock(control.Page.GetType(), "Refresh", GetRefreshScript());
HookupScriptToControl(control, "javascript:return(Refresh())", "OnClick");
}
///
/// Shows a modeless popup window for the specified web page when the has loaded.
///
/// that will show the popup window when loaded.
/// Web page url for which the popup windows is to be shown.
/// Height of the popup window.
/// Width of the popup window.
/// Location of the popup window on X-axis.
/// Location of the popup window on Y-axis.
/// True if the popup window is to be centered, otherwise False.
/// True if help button is to be displayed, otherwise False.
/// True if the popup window can be resized, otherwise False.
/// True if the status bar is to be displayed, otherwise False.
public static void Show(this Page page, string url, int height = 400, int width = 600, int left = 0, int top = 0, bool center = true, bool help = false, bool resizable = false, bool status = false)
{
if (!page.ClientScript.IsClientScriptBlockRegistered("Show"))
page.ClientScript.RegisterClientScriptBlock(page.GetType(), "Show", GetShowScript());
if (!page.ClientScript.IsStartupScriptRegistered("Show." + url))
{
StringBuilder script = new StringBuilder();
script.Append("\r\n");
page.ClientScript.RegisterStartupScript(page.GetType(), "Show." + url, script.ToString());
}
}
///
/// Shows a modeless popup window for the specified web page when the is clicked.
///
/// that will show the popup window when clicked.
/// Web page url for which the popup windows is to be shown.
/// Height of the popup window.
/// Width of the popup window.
/// Location of the popup window on X-axis.
/// Location of the popup window on Y-axis.
/// True if the popup window is to be centered, otherwise False.
/// True if help button is to be displayed, otherwise False.
/// True if the popup window can be resized, otherwise False.
/// True if the status bar is to be displayed, otherwise False.
public static void Show(this Control control, string url, int height = 400, int width = 600, int left = 0, int top = 0, bool center = true, bool help = false, bool resizable = false, bool status = false)
{
if (!control.Page.ClientScript.IsClientScriptBlockRegistered("Show"))
control.Page.ClientScript.RegisterClientScriptBlock(control.Page.GetType(), "Show", GetShowScript());
HookupScriptToControl(control, "javascript:return(Show('" + url + "', " + height + ", " + width + ", " + left + ", " +
top + ", " + Math.Abs(Convert.ToInt32(center)) + ", " + Math.Abs(Convert.ToInt32(help)) + ", " +
Math.Abs(Convert.ToInt32(resizable)) + ", " + Math.Abs(Convert.ToInt32(status)) + "))", "OnClick");
}
///
/// Shows a modal popup window for the specified web page when the has loaded.
///
/// Web page that will show the popup window when loaded.
/// Web page url for which the popup windows is to be shown.
/// whose text is to updated with the text returned by the web page displayed in the popup window.
/// Height of the popup window.
/// Width of the popup window.
/// Location of the popup window on X-axis.
/// Location of the popup window on Y-axis.
/// True if the popup window is to be centered, otherwise False.
/// True if help button is to be displayed, otherwise False.
/// True if the popup window can be resized, otherwise False.
/// True if the status bar is to be displayed, otherwise False.
public static void ShowDialog(this Page page, string url, Control dialogResultHolder = null, int height = 400, int width = 600, int left = 0, int top = 0, bool center = true, bool help = false, bool resizable = false, bool status = false)
{
if (!page.ClientScript.IsClientScriptBlockRegistered("ShowDialog"))
page.ClientScript.RegisterClientScriptBlock(page.GetType(), "ShowDialog", GetShowDialogScript());
if (!page.ClientScript.IsStartupScriptRegistered("ShowDialog." + url))
{
StringBuilder script = new StringBuilder();
script.Append("\r\n");
script.Append("\r\n");
script.Append("\r\n");
page.ClientScript.RegisterStartupScript(page.GetType(), "ShowDialog." + url, script.ToString());
}
}
///
/// Shows a modal popup window for the specified web page when the is clicked.
///
/// that will show the popup window when clicked.
/// Web page url for which the popup windows is to be shown.
/// whose text is to updated with the text returned by the web page displayed in the popup window.
/// Height of the popup window.
/// Width of the popup window.
/// Location of the popup window on X-axis.
/// Location of the popup window on Y-axis.
/// True if the popup window is to be centered, otherwise False.
/// True if help button is to be displayed, otherwise False.
/// True if the popup window can be resized, otherwise False.
/// True if the status bar is to be displayed, otherwise False.
public static void ShowDialog(this Control control, string url, Control dialogResultHolder = null, int height = 400, int width = 600, int left = 0, int top = 0, bool center = true, bool help = false, bool resizable = false, bool status = false)
{
if (!control.Page.ClientScript.IsClientScriptBlockRegistered("ShowDialog"))
control.Page.ClientScript.RegisterClientScriptBlock(control.Page.GetType(), "ShowDialog", GetShowDialogScript());
if (dialogResultHolder != null)
{
HookupScriptToControl(control, "javascript:return(ShowDialog('" + url + "', '" + dialogResultHolder.ClientID + "', " +
height + ", " + width + ", " + left + ", " + top + ", " +
Math.Abs(Convert.ToInt32(center)) + ", " + Math.Abs(Convert.ToInt32(help)) + ", " +
Math.Abs(Convert.ToInt32(resizable)) + ", " + Math.Abs(Convert.ToInt32(status)) + "))", "OnClick");
}
else
{
HookupScriptToControl(control, "javascript:return(ShowDialog('" + url + "', null, " + height + ", " + width + ", " + left + ", " + top + ", " +
Math.Abs(Convert.ToInt32(center)) + ", " + Math.Abs(Convert.ToInt32(help)) + ", " +
Math.Abs(Convert.ToInt32(resizable)) + ", " + Math.Abs(Convert.ToInt32(status)) + "))", "OnClick");
}
}
///
/// Shows a popup for the specified web page when the has loaded.
///
/// that will show the popup when loaded.
/// Web page url for which the popup is to be shown.
/// Height of the popup.
/// Width of the popup.
/// Location of the popup on X-axis.
/// Location of the popup on Y-axis.
/// True if the popup is to be centered, otherwise False.
/// True if the popup can be resized, otherwise False.
/// True is the scrollbars are to be displayed, otherwise False.
/// True if the toolbar is to be displayed, otherwise False.
/// True if the menu bar is to be displayed, otherwise False.
/// True if the address bar is to be displayed, otherwise False.
/// True if the status bar is to be displayed, otherwise False.
/// True if the directories buttons (Netscape only) are to be displayed, otherwise False.
public static void ShowPopup(this Page page, string url, int height = 400, int width = 600, int left = 0, int top = 0, bool center = true, bool resizable = false, bool scrollbars = false, bool toolbar = false, bool menubar = false, bool location = false, bool status = false, bool directories = false)
{
if (!page.ClientScript.IsClientScriptBlockRegistered("ShowPopup"))
page.ClientScript.RegisterClientScriptBlock(page.GetType(), "ShowPopup", GetShowPopupScript());
if (!page.ClientScript.IsStartupScriptRegistered("ShowPopup." + url))
{
StringBuilder script = new StringBuilder();
script.Append("\r\n");
page.ClientScript.RegisterStartupScript(page.GetType(), "ShowPopup." + url, script.ToString());
}
}
///
/// Shows a popup for the specified web page when the is clicked.
///
/// that will show the popup when clicked.
/// Web page url for which the popup is to be shown.
/// Height of the popup.
/// Width of the popup.
/// Location of the popup on X-axis.
/// Location of the popup on Y-axis.
/// True if the popup is to be centered, otherwise False.
/// True if the popup can be resized, otherwise False.
/// True is the scrollbars are to be displayed, otherwise False.
/// True if the toolbar is to be displayed, otherwise False.
/// True if the menu bar is to be displayed, otherwise False.
/// True if the address bar is to be displayed, otherwise False.
/// True if the status bar is to be displayed, otherwise False.
/// True if the directories buttons (Netscape only) are to be displayed, otherwise False.
public static void ShowPopup(this Control control, string url, int height = 400, int width = 600, int left = 0, int top = 0, bool center = true, bool resizable = false, bool scrollbars = false, bool toolbar = false, bool menubar = false, bool location = false, bool status = false, bool directories = false)
{
if (!control.Page.ClientScript.IsClientScriptBlockRegistered("ShowPopup"))
control.Page.ClientScript.RegisterClientScriptBlock(control.Page.GetType(), "ShowPopup", GetShowPopupScript());
HookupScriptToControl(control, "javascript:return(ShowPopup('" + url + "', " + height + ", " + width + ", " + left + ", " + top + ", " +
Math.Abs(Convert.ToInt32(center)) + ", " + Math.Abs(Convert.ToInt32(resizable)) + ", " +
Math.Abs(Convert.ToInt32(scrollbars)) + ", " + Math.Abs(Convert.ToInt32(toolbar)) + ", " +
Math.Abs(Convert.ToInt32(menubar)) + ", " + Math.Abs(Convert.ToInt32(location)) + ", " +
Math.Abs(Convert.ToInt32(status)) + ", " + Math.Abs(Convert.ToInt32(directories)) + "))", "OnClick");
}
///
/// Closes the current when it has finished loading in the browser and returns the specified to the web page that opened it.
///
/// to be closed.
/// Value to be returned to the parent web page.
public static void Close(this Page page, string returnValue = null)
{
if (!page.ClientScript.IsClientScriptBlockRegistered("Close"))
page.ClientScript.RegisterClientScriptBlock(page.GetType(), "Close", GetCloseScript());
if (!page.ClientScript.IsStartupScriptRegistered("Close.Page"))
{
StringBuilder script = new StringBuilder();
script.Append("\r\n");
page.ClientScript.RegisterStartupScript(page.GetType(), "Close.Page", script.ToString());
}
}
///
/// Closes the current page when the is clicked and returns the specified to the web page that opened it.
///
/// that will close the current web page when clicked.
/// Value to be returned to the parent web page.
public static void Close(this Control control, string returnValue = null)
{
if (!control.Page.ClientScript.IsClientScriptBlockRegistered("Close"))
control.Page.ClientScript.RegisterClientScriptBlock(control.Page.GetType(), "Close", GetCloseScript());
HookupScriptToControl(control, "javascript:return(Close('" + returnValue + "'))", "OnClick");
}
///
/// Shows a windows application style message box when the has loaded.
///
/// that will show the message box when loaded.
/// Text that is to be displayed in the message box.
/// Title of the message box.
/// Buttons to be displayed in the message box.
/// True if a post-back is to be performed when either OK, Retry, or Yes buttons are clicked in the message box, otherwise False.
public static void MsgBox(this Page page, string prompt, string title, MsgBoxStyle buttons, bool doPostBack = false)
{
if (!page.ClientScript.IsClientScriptBlockRegistered("ShowMsgBox"))
page.ClientScript.RegisterClientScriptBlock(page.GetType(), "ShowMsgBox", GetMsgBoxScript());
if (!page.ClientScript.IsStartupScriptRegistered("ShowMsgBox." + prompt))
{
StringBuilder script = new StringBuilder();
script.Append("\r\n");
script.Append("\r\n");
script.Append("\r\n");
page.ClientScript.RegisterStartupScript(page.GetType(), "ShowMsgBox." + prompt, script.ToString());
}
}
///
/// Shows a windows application style message box when the is clicked.
///
/// that will show the message box when clicked.
/// Text that is to be displayed in the message box.
/// Title of the message box.
/// Buttons to be displayed in the message box.
/// True if a post-back is to be performed when either OK, Retry, or Yes buttons are clicked in the message box, otherwise False.
public static void MsgBox(this Control control, string prompt, string title, MsgBoxStyle buttons, bool doPostBack = false)
{
if (!control.Page.ClientScript.IsClientScriptBlockRegistered("ShowMsgBox"))
control.Page.ClientScript.RegisterClientScriptBlock(control.Page.GetType(), "ShowMsgBox", GetMsgBoxScript());
HookupScriptToControl(control, "javascript:return(ShowMsgBox('" + JavaScriptEncode(ref prompt) + " ', '" + title + "', " + (int)buttons + ", " + doPostBack.ToString().ToLower() + "))", "OnClick");
}
private static void HookupScriptToControl(Control control, string script, string attribute)
{
if (control is Button)
((Button)control).Attributes.Add(attribute, script);
else if (control is LinkButton)
((LinkButton)control).Attributes.Add(attribute, script);
else if (control is ImageButton)
((ImageButton)control).Attributes.Add(attribute, script);
else if (control is Image)
((Image)control).Attributes.Add(attribute, script);
else if (control is Label)
((Label)control).Attributes.Add(attribute, script);
else if (control is TextBox)
((TextBox)control).Attributes.Add(attribute, script);
}
private static string GetRefreshScript()
{
StringBuilder script = new StringBuilder();
script.Append("\r\n");
return script.ToString();
}
private static string GetShowScript()
{
StringBuilder script = new StringBuilder();
script.Append("\r\n");
return script.ToString();
}
private static string GetShowDialogScript()
{
StringBuilder script = new StringBuilder();
script.Append("\r\n");
return script.ToString();
}
private static string GetShowPopupScript()
{
StringBuilder script = new StringBuilder();
script.Append("\r\n");
return script.ToString();
}
private static string GetCloseScript()
{
StringBuilder script = new StringBuilder();
script.Append("\r\n");
return script.ToString();
}
private static string GetMsgBoxScript()
{
StringBuilder script = new StringBuilder();
script.Append("\r\n");
return script.ToString();
}
#region [ Old Code ]
/////
///// The different types of client-side scripts.
/////
/////
//private enum ClientSideScript
//{
// Focus,
// DefaultButton,
// Show,
// ShowDialog,
// ShowPopup,
// Close,
// MsgBox,
// Refresh,
// Maximize,
// Minimize,
// RunClientExe
//}
/////
///// Creates the appropriate client-side script based on the specified PCS.Web.ClientSideScript value.
/////
///// One of the PCS.Web.ClientSideScript values.
///// The client-side script for the specified PCS.Web.ClientSideScript value
/////
//private static string CreateClientSideScript(ClientSideScript script)
//{
// string clientScript = "";
// switch (script)
// {
// case ClientSideScript.Focus: // Client-side script for Focus.
// System.Text.StringBuilder with_1 = new StringBuilder();
// with_1.Append("" + System.Environment.NewLine);
// clientScript = with_1.ToString();
// break;
// case ClientSideScript.DefaultButton: // Client-side script for DefaultButton.
// System.Text.StringBuilder with_2 = new StringBuilder();
// with_2.Append("" + System.Environment.NewLine);
// clientScript = with_2.ToString();
// break;
// case ClientSideScript.Show: // Client-side script for Show.
// System.Text.StringBuilder with_3 = new StringBuilder();
// with_3.Append("" + System.Environment.NewLine);
// clientScript = with_3.ToString();
// break;
// case ClientSideScript.ShowDialog: //Client-side script for ShowDialog.
// System.Text.StringBuilder with_4 = new StringBuilder();
// with_4.Append("" + System.Environment.NewLine);
// clientScript = with_4.ToString();
// break;
// case ClientSideScript.ShowPopup: // Client-side script for ShowPopup.
// System.Text.StringBuilder with_5 = new StringBuilder();
// with_5.Append("" + System.Environment.NewLine);
// clientScript = with_5.ToString();
// break;
// case ClientSideScript.Close: // Client-side script for Close.
// System.Text.StringBuilder with_6 = new StringBuilder();
// with_6.Append("" + System.Environment.NewLine);
// clientScript = with_6.ToString();
// break;
// case ClientSideScript.MsgBox: // Client-side script for MsgBox.
// System.Text.StringBuilder with_7 = new StringBuilder();
// with_7.Append("" + System.Environment.NewLine);
// clientScript = with_7.ToString();
// break;
// case ClientSideScript.Refresh: // Client-side script for Refresh.
// System.Text.StringBuilder with_8 = new StringBuilder();
// with_8.Append("" + System.Environment.NewLine);
// clientScript = with_8.ToString();
// break;
// case ClientSideScript.Maximize: // Client-side script for Maximize.
// System.Text.StringBuilder with_9 = new StringBuilder();
// with_9.Append("" + System.Environment.NewLine);
// clientScript = with_9.ToString();
// break;
// case ClientSideScript.Minimize: // Client-side script for Minimize.
// System.Text.StringBuilder with_10 = new StringBuilder();
// with_10.Append("" + System.Environment.NewLine);
// clientScript = with_10.ToString();
// break;
// case ClientSideScript.RunClientExe: // Client-side script for RunClientExe.
// System.Text.StringBuilder with_11 = new StringBuilder();
// with_11.Append("" + System.Environment.NewLine);
// clientScript = with_11.ToString();
// break;
// }
// return clientScript;
//}
#endregion
}
}