views:

233

answers:

3

I have a dynamically created menu which I have a menuItemClick event tied in. In this event I want to execute a simple window.open script that uses the clicked items' url. The reason I need to do this is I need to set the parameters of the window opening (i.e. no scroll bars, no toolbars, etc...).

My question is, is there a way simply create a string of the script to be executed and then actually execute it?

So far I have:

string script = "window.open('" + e.Item.NavigateUrl + "' ,'_MAIN_WINDOW','width='+ screen.width + '-60,height=' + screen.height + '-500,left=' + screen.left + '-30,top=' + screen.top + '-30,screenx=0,screeny=0,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,copyhistory=no, resizable=yes')";

But I have no idea how to then execute this script.

A: 

You'll want to use Page.ClientScript.RegisterStartupScript (or ScriptManager.RegisterStartupScript if you need it to run after a partial postback).

bdukes
I tried this one but I need to execute it at the time of the ItemClick. When I register it at the time of itemClick then it doesn't execute until the next postback.
Collin Estes
Does ItemClick not cause a postback? If it's all on the client, what about menuItem.Attributes["onclick"] = script; ?
bdukes
A: 

Override the OnPreRender event of the page.

Inside there you make:

protected override void OnPreRender(EventArgs e)
{
  base.OnPreRender(e);  


  foreach (MenuItem item in m1.Items)  // m1 is the menu in my case :)
  {
    item.Target = string.Format("window.open('{0}');", item.NavigateUrl);
  }
}
Greco
+1  A: 

You can put the window.open (...) code inside a javascript function called onButtonClick (item) and emit it with RegisterStartupScript () just as bdukes suggested, then in the aspx code for the page you can add an eventHandler like

<a id="bla" onclick="javascript:onButtonClick(this);" >Dummy item</a>

Then when you click on the item, onButtonClick will be called with the clicked item as parameter.

axel_c