【问题标题】:Executing a simple window.open script from the server从服务器执行一个简单的 window.open 脚本
【发布时间】:2009-01-12 17:46:15
【问题描述】:

我有一个动态创建的菜单,其中绑定了一个 menuItemClick 事件。在这种情况下,我想执行一个简单的 window.open 脚本,该脚本使用点击的项目的 url。我需要这样做的原因是我需要设置窗口打开的参数(即没有滚动条,没有工具栏等......)。

我的问题是,有没有办法简单地创建一个要执行的脚本字符串然后实际执行它?

到目前为止,我有:

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')";

但我不知道如何执行这个脚本。

【问题讨论】:

    标签: c# asp.net javascript


    【解决方案1】:

    您可以将 window.open (...) 代码放在一个名为 onButtonClick (item) 的 javascript 函数中,并按照 bdukes 的建议使用 RegisterStartupScript () 发出它,然后在页面的 aspx 代码中添加一个 eventHandler喜欢

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

    那么当你点击item的时候,onButtonClick就会被调用,并且被点击的item作为参数。

    【讨论】:

      【解决方案2】:

      您需要使用Page.ClientScript.RegisterStartupScript(或ScriptManager.RegisterStartupScript,如果您需要在部分回发后运行它)。

      【讨论】:

      • 我试过这个但是我需要在ItemClick的时候执行它。当我在 itemClick 时注册它时,它直到下一次回发才会执行。
      • ItemClick 不会导致回发吗?如果都是在客户端,那么 menuItem.Attributes["onclick"] = script; ?
      【解决方案3】:

      覆盖页面的 OnPreRender 事件。

      你在里面做:

      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);
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-01-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多