Adding JavaScript to ASP.NET Menu control to open a popup window + centered on screen
September 14, 2007
Finally this is my first post about coding on C#, i hope this helps somebody (it worked for me), at least for reference.
The past week ive been working on an ASP.NET app which requires to show a many forms as a PopUp but they needed to be loaded with the Menu control provided on the framework. The control is databound to a sitemap XML file.
Was a little complicated because it needs few steps to make it work but i hope this may help you to make the same and save you few hours trying to figure out how to.
Lets start…
Rigth click over your solution tree, select add new item, and from the window choose a “site map”, then click on OK.
On the file generated you need to add the corresponding entries to your page, but we will use mor XML fields to fulfill our purpuose:
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0">
<siteMapNode title="Root Menu" description="This is the root menu">
<siteMapNode title="Child Menu" description="This is a child menu">
<siteMapNode url="" title="Click on me to open a popup window"
description="This page is cool" heigth="450" width="screen.width - 150"
address="popupme.aspx"/>
<siteMapNode url="form.aspx" title="Click on me to open a normal window"
description="This page is cool"/>
</siteMapNode>
</siteMapNode>
</siteMap>
Once you write the entries, click on save and close it, now look on the menu’s event “MenuItemDataBound”, and write the following on it:
void menuObject_MenuItemDataBound(object sender, MenuEventArgs e)
{
string address = string.Empty;
string heigth = string.Empty;
string width = string.Empty;
string title = string.Empty;
string url = string.Empty;
address = ((SiteMapNode)(e.Item.DataItem))["address"];
heigth = ((SiteMapNode)(e.Item.DataItem))["heigth"];
width = ((SiteMapNode)(e.Item.DataItem))["width"];
title = ((SiteMapNode)(e.Item.DataItem))["title"];
url = ((SiteMapNode)(e.Item.DataItem))["url"];
//we left the url field empty on the sitemap file to know
//this will be a javascript link, so:
if (string.IsNullOrEmpty(url))
{
e.Item.NavigateUrl = "javascript:OpenWindow('" + address + "'," + heigth + "," + width + ",'" + title + "')";
}
else
{
e.Item.NavigateUrl = url;
}
}
Now we are close to the end.. did you noticed there was something missing??!!… yes!, the javascript function to open the popup window, here is the source:
(Dont forget to put it inside the page which will launch the popup windows (the page with the menu control, or the masterpage of it) inside the <HEADER>…</HEADER> tags!
<script language="javascript">;
function OpenWindow(address, heigth, width, title)
{
var winl = (screen.width-width)/2;
var wint = (screen.height-heigth)/2;
var options = "width=" + width;
options += ",height=" + heigth;
options += ",top=" + wint;
options += ",left=" + winl;
options += ",location=no,toolbar=no, menubar=no, scrollbars=1, resizable";
window.open(address, title, options);
}
</script>;
Once when you have this, we need to add a default sitemap provider for the project so the last step is modify your web.config file as its shown here inside the system.web sections:
<system.web>
<siteMap defaultProvider="XmlSiteMapProvider" enabled="true">
<providers>
<add name="XmlSiteMapProvider" type="System.Web.XmlSiteMapProvider" siteMapFile="Web.sitemap"/>
</providers>
</siteMap>
</system.web>
Now, you can try to run your App!.
Again this is my first Code post and i hope this helps you, any comments are welcome!
















May 22, 2008 at 1:11 pm
Isn’t a simpler solution to just do this:
It won’t be centered on the screen, but it’s easy.
June 15, 2008 at 9:06 am
This is all I did:
and I had the default master reference the js file with the function. Am I missing something?
August 7, 2008 at 2:19 am
If i want to open the pop up only for a specific menu item, what should i do??
August 15, 2008 at 12:18 am
deep,
Look at the second code box on this post, on this example we know that the empty “url” means to use javascript.
On the first code box, you can see we left the url field empty, in order to identify it later:
<siteMapNode url=”" title=”Click
Then, while binding the XML data to the control, look at the property “NavigateUrl”, we are setting JS to open the link in a popup instead the common URL.
August 19, 2008 at 3:22 am
I follow what you gave but i still cannot pop up a page from sitemap menu.
can i know what happen?
August 19, 2008 at 9:44 am
Calvin,
Ensure the link adress of your sitemap menu are pointing to a javascript function (put your pointer over the menu and watch the status bar, it should say something like “javascript:…”.
If the last step is ok, then be sure about you have the JavaScript function between the tags on your webpage or masterpage.
regards.
August 25, 2008 at 12:29 am
still cannot,
when i click but the firest selection, it does not have any action? I am using VS 2005 VB.net. can you send me the example file?
Thanks
August 31, 2008 at 11:55 pm
Good day!,
September 22, 2009 at 10:16 pm
Hi founder,
May i know where should i put the void menuObject_MenuItemDataBound(object sender, MenuEventArgs e)
In the master page? or where should i paste this code?
Thanks