Using ASP.Net to open a new browser window - Part II, the web control

by jrummell 20. February 2007 13:24

This a follow up to my previous post, Using ASP.Net to open a new browser window - Part I. There I used a static helper class to register a javascript function to open a new browser window. I've scratched the static class and replaced it with a BrowserWindow class and a PopUpWindow WebControl. BrowserWindow simply encapsulates all of the parameters passed to RegisterOpenWindowScript(), and PopUpWindow registers the javascript function and the call to the function.

Here's an example:



<cc1:PopUpWindow ID="PopUpWindow1" runat="server" OpenOnLoad="false" />
<asp:Button ID="Button1" runat="server" Text="Open Window" OnClick="Button1_Click" />



protected void Page_Load(object sender, EventArgs e)
{
BrowserWindow window = new BrowserWindow(
"http://john.rummell.info/blog", 800, 600);
window.Resizable = true;
window.Scrollbars = true;
PopUpWindow1.BrowserWindow = window;
}
protected void Button1_Click(object sender, EventArgs e)
{
PopUpWindow1.OpenWindow();
}



PopUpWindow exposes a few of BrowserWindow's properties: Width, Height, and Url. For more options, create a BrowserWindow object and set PopUpWindow's BroswerWindow property with it, as shown in Page_Load. You can use the OpenWindow() method of PopUpWindow to open the window as shown in Button1_Click, or you can set OpenOnLoad to true to have it open when the page loads.

BrowserWindow.cs (7.56 kb), PopUpWindow.cs (6.91 kb)

 

Tags: ,

asp.net

Using ASP.Net to open a new browser window - Part I

by jrummell 19. February 2007 23:15

I recently needed to add a 'pop-up' window to a website. Now I'm not a fan of javascript, never have been. I like my code to be type safe and compile. So I came up with a C# helper class to do the dirty work for me. I must give credit to where I found the idea. I saw something like this in Chapter 4 of Developing Web Applications with Microsoft VB .Net and VC# .Net by Jeff Webb and MS Corp. In this book they create BrowserWindow class and use it inside some javascript. I've take a different approach, so that I can avoid having to write the javascript in the future.

I created a static helper class that contains the following method. I also provided some methods that overload this, but you get the idea.


public static void RegisterOpenWindowScript(
Page page, string key, string url,
WindowName name, int width, int height,
int left, int top,
bool location, bool menubar, bool resizable,
bool scrollbars, bool status, bool titlebar)
{
if (!page.ClientScript.IsClientScriptBlockRegistered(
typeof(OpenWindowHelper), 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();
}}
", url, name, width, height, left, top,
GetInt(location), GetInt(menubar),
GetInt(resizable), GetInt(scrollbars), GetInt(status),                    
GetInt(titlebar), openFunctionName);
page.ClientScript.RegisterClientScriptBlock(
typeof(OpenWindowHelper), key, script, true);
}
}


WindowName is an enum that defines the possible window names: _blank, _parent, _self, _top. GetInt() simply returns 1 if true and 0 if false.

As I'm writing this I'm realizing that it would be a cool idea to merge this static class with the BrowserWindow class. You could define a BrowserWindow object and perform the Open() operation to open the window. Hmm ... I'm seeing a follow up post in the near future.


OpenWindowHelper.cs (5.24 kb)

Tags:

asp.net

Powered by BlogEngine.NET 1.6.1.0
Theme by Mads Kristensen | Modified by Mooglegiant

About the author

John is a .Net Web Developer for a manufacturing company in Ohio. In his free time he enjoys web development, the outdoors, and spending time with his wife.

Ads By Google

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in  anyway.

© Copyright 2010