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

Comments

9/18/2009 2:11:31 PM #

chain tool

Thanks for this, just what ive been looking for!

chain tool United Kingdom

Comments are closed

Powered by BlogEngine.NET 1.5.0.7
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.

Disclaimer

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

© Copyright 2009

Ads By Google