using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace JRummell.Web.Controls
{
[DefaultProperty("BrowserWindow")]
[ToolboxData("<{0}:PopUpWindow Url=\"\" WindowWidth=\"\" WindowHeight=\"\" OpenOnLoad=false runat=server/>")]
public class PopUpWindow : WebControl
{
#region Fields
private static string openFunctionName = "openWindow";
#endregion
#region Constructor
///
/// Initializes a new instance of the class.
///
public PopUpWindow()
{
BrowserWindow = new BrowserWindow();
EnableViewState = false;
Load += new EventHandler(PopUpWindow_Load);
}
#endregion
#region Events
///
/// Handles the Load event of the PopUpWindow control.
///
/// The source of the event.
/// The
/// instance containing the event data.
private void PopUpWindow_Load(object sender, EventArgs e)
{
RegisterFunctionScript();
if (OpenOnLoad)
{
RegisterFunctionCallScript();
}
}
///
/// Renders the contents of the control to the specified writer.
/// This method is used primarily by control developers.
///
/// A
/// that represents the output stream to render HTML content on the client.
protected override void RenderContents(HtmlTextWriter writer)
{
// render nothing
}
#endregion
#region Properties
///
/// Gets or sets the browser window.
///
/// The browser window.
[Bindable(true)]
[Category("Data")]
public BrowserWindow BrowserWindow
{
get
{
return (BrowserWindow)ViewState["BrowserWindow"];
}
set
{
ViewState["BrowserWindow"] = value;
}
}
///
/// Gets the target URL.
///
/// The target URL.
[Category("Behavior")]
[DefaultValue("")]
public string Url
{
get
{
return BrowserWindow.Url;
}
set
{
BrowserWindow.Url = value;
}
}
///
/// Gets or sets the width of the window.
///
/// The width of the window.
[Category("Behavior")]
[DefaultValue(400)]
public int WindowWidth
{
get
{
return BrowserWindow.Width;
}
set
{
BrowserWindow.Width = value;
}
}
///
/// Gets or sets the height of the window.
///
/// The height of the window.
[Category("Behavior")]
[DefaultValue(200)]
public int WindowHeight
{
get
{
return BrowserWindow.Height;
}
set
{
BrowserWindow.Height = value;
}
}
///
/// Gets or sets a value indicating whether the window should open on load.
///
/// if true opens on the window on load.
[Category("Behavior")]
[DefaultValue(false)]
public bool OpenOnLoad
{
get
{
object obj = (bool)ViewState["OpenOnLoad"];
return (obj == null) ? false : (bool)obj;
}
set
{
ViewState["OpenOnLoad"] = value;
}
}
///
/// Gets the open window client function call.
///
/// The open window client function call.
public string OpenFunctionCall
{
get
{
return openFunctionName + "();";
}
}
#endregion
#region Public Methods
///
/// Opens the window.
///
public void OpenWindow()
{
RegisterFunctionCallScript();
}
#endregion
#region Private Methods
///
/// Registers the open window function client script.
///
private void RegisterFunctionScript()
{
string key = this.GetType().ToString();
if (!Page.ClientScript.IsClientScriptBlockRegistered(
this.GetType(), key))
{
string script = String.Format(@"
function {12}()
{{
var win = window.open('{0}', '{1}', 'width={2},height={3},left={4},top={5},location={6},menubar={7},resizable={8},scrollbars={9},status={10},titlebar={11}');
win.focus();
}}
", BrowserWindow.Url, BrowserWindow.Name, BrowserWindow.Width, BrowserWindow.Height,
BrowserWindow.Left, BrowserWindow.Top, GetInt(BrowserWindow.Location),
GetInt(BrowserWindow.Menubar), GetInt(BrowserWindow.Resizable),
GetInt(BrowserWindow.Scrollbars), GetInt(BrowserWindow.Status),
GetInt(BrowserWindow.Titlebar), openFunctionName);
Page.ClientScript.RegisterClientScriptBlock(
this.GetType(), key, script, true);
}
}
///
/// Registers the open window function call client script.
///
private void RegisterFunctionCallScript()
{
string key = this.GetType().ToString() + "_call";
if (!Page.ClientScript.IsClientScriptBlockRegistered(key))
{
Page.ClientScript.RegisterClientScriptBlock(
this.GetType(), key,
OpenFunctionCall, true);
}
}
///
/// Gets an int from a boolean where true=1 and false=0.
///
/// if set to true 1; else 0.
private static int GetInt(bool val)
{
return val ? 1 : 0;
}
#endregion
}
}