I was searching for a quick way to add a customizable popup into a
Now that I have used it and am happy with the results, I thought I would share this with the world, just in case it could save someone else some time!
Place the code in your Helper.cs class (that's where I place it so I can reuse it over the whole site), or just put it in the background code.
public static string CreatePopupUrl(double windowHeight,
params object args)
string strShowScrollbars = showScrollbars ? "yes" : "no";
string strShowMenuBar = showMenuBar ? "yes" : "no";
string strIsResizable = isResizable ? "yes" : "no";
string strShowToolbar = showToolbar ? "yes" : "no";
string strHasLocation = hasLocation ? "yes" : "no";
string strShowStatus = showStatus ? "yes" : "no";
url = string.Format(url, args);
double finalLeftPosition = leftPosition;
double finalTopPosition = topPosition;
if (leftPosition == -1)
if (Screen.PrimaryScreen != null)
(((double)Screen.PrimaryScreen.Bounds.Width / 2) -
(windowWidth / 2));
finalLeftPosition = 0;
if (topPosition == -1)
if (Screen.PrimaryScreen != null)
(((double)Screen.PrimaryScreen.Bounds.Height / 2) -
(windowHeight / 2));
finalTopPosition = 0;
string str = "window.open('" + url + "','','scrollbars=" + strShowScrollbars +
",menubar=" + strShowMenuBar +
",height=" + windowHeight +
",width=" + windowWidth +
",resizable=" + strIsResizable +
",toolbar=" + strShowToolbar +
",location=" + strHasLocation +
",status=" + strShowStatus +
",left=" + finalLeftPosition +
",top=" + finalTopPosition + "')";
How I Use it
The code below is just to show where I put it in a
GridView. As you can see, I put it in the
OnClientClick area. I found this was where it worked best for me. I use it mainly for sending encrypted text to a page, but below, I have just used Google Video and Maps as an example.
As you can see, I have used -1 for Left position and -1 for Top position. This is how I can tell the code to try and centre the popup window in the screen.
If I send a location like 30 and 30, then it would be 30 pixels in and 30 pixels down from the top left corner of the screen.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Visible="True"
CellPadding="4" ForeColor="#333333" GridLines="None">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<asp:LinkButton ID="LbtnGoogle1" runat= "server"
, -1 //left position
, -1 //top position
<asp:LinkButton ID="LbtnGoogle2" runat= "server"
, 30 // left position
, 30 // top position
I hope that someone will find this helpful.
- 15th October, 2009: Initial post
- 16th October, 2009: Added source code
- 19th October, 2009: Updated source code
My first memories of programming were back in the days of Amstrad 464+, and we used to get a magazine called "Amstrad Action", every issue had some games or programs you could code into the computer and I used to love doing this.
I remember watching "War Games" at the same time as typing in the code to a small game on the computer, and thought it would be amazing to be able to connect the computer to the telephone!
In 2000, I started a computing A Level, part of the course was Delphi Programming, and I remember writing an application to display train time tables and ticket costs, and creating a manual of how to use it, and the lecturer said to me back then I would have a career as a Manual writer or a programmer. (I chose Programmer)
The programming lecturer was a lively, wacky lecturer, and that made it entertaining.
I then went back to night school in 2006 and learnt VB.NET.
This lecturer (at a different college, in a different country was also quite wacky and lively (seemed to be a common occurrence))
During the second year at night school my employer asked if I could put together a quotation application, I said I was up for it and the MD and I spent a day or two a week looking into the calculations, and after a few months we had created a quotation application that they still use to this day!
I created 4 more applications before joining my previous employer in 2008.
At my previous employment I created Mobile Software for Windows Mobile 6.1, Integrated with Sage Line 50, Exchequer, created ASP.NET websites, SSRS Reports for Customers and Internal use, created the UI for the new Scheduling application, and more.
Now working for Gemba Solutions Ltd as a Product Development Support Engineer.