【问题标题】:Redirecting new tab on button click.(Response.Redirect) in asp.net C#在 asp.net C# 中单击按钮时重定向新选项卡。(Response.Redirect)
【发布时间】:2011-06-06 06:56:03
【问题描述】:

我正在尝试在单击按钮时在新选项卡/窗口中打开一个页面。我在 google 中尝试了此代码,但它不起作用。

谁能帮我解决这个问题?

<asp:Button ID="btn" runat="Server" Text="SUBMIT" 
     OnClick="btnNewEntry_Click" OnClientClick="aspnetForm.target ='_blank';"/>

protected void btnNewEntry_Click(object sender, EventArgs e)
{
    Response.Redirect("CMS_1.aspx");
}

当我使用这个时,我收到错误提示

   Microsoft JScript runtime error: 'aspnetForm' is undefined.

【问题讨论】:

  • 您的表单 ID 是什么?您是否尝试将OnClientClick="aspnetForm.target..." 中的“aspnetForm”更改为您的&lt;form&gt; 的ID?

标签: c# asp.net response.redirect targets


【解决方案1】:

你可以这样做:

<asp:Button ID="Button1" runat="server" Text="Button"
    onclick="Button1_Click" OnClientClick="document.forms[0].target = '_blank';" />

【讨论】:

    【解决方案2】:

    你会不会更好

    <asp:HyperLink ID="HyperLink1" runat="server" 
                NavigateUrl="CMS_1.aspx" 
                Target="_blank">
        Click here
    </asp:HyperLink>
    

    因为,要在asp:Button 上复制您想要的行为,您必须在按钮的OnClientClick 事件上调用window.open,这看起来比上述解决方案要干净得多。加上asp:HyperLink 可以处理这样的场景。

    如果您想使用 asp:Button 复制此内容,请执行此操作。

    <asp:Button ID="btn" runat="Server" 
            Text="SUBMIT"
            OnClientClick="javascript:return openRequestedPopup();"/>
    

    JavaScript 函数。

    var windowObjectReference;
    
    function openRequestedPopup() {
        windowObjectReference = window.open("CMS_1.aspx",
                  "DescriptiveWindowName",
                  "menubar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes");
    }
    

    【讨论】:

    • 这个我知道了。有没有办法处理Button?。
    【解决方案3】:

    我认为您的代码应该可以工作,只需从此处删除一件事,但它会从现有窗口内的当前页面进行重定向

    <asp:Button ID="btn" runat="Server" Text="SUBMIT" 
    
     OnClick="btnNewEntry_Click"/>    
    
    
    
    protected void btnNewEntry_Click(object sender, EventArgs e)
     {
        Response.Redirect("CMS_1.aspx");
     }
    

    如果你想通过客户端脚本来做到这一点 用这种方式

    <asp:Button ID="BTN" runat="server" Text="Submit" OnClientClick="window.open('Default2.aspx')" />
    

    根据我的说法,您应该更喜欢客户端脚本,因为只是打开一个新窗口服务器端就会回发一个帖子,这将是无用的..

    【讨论】:

    • 它正在新窗口中打开窗口。我可以在新标签中执行此操作吗。
    • window.open 的签名是 var windowObjectReference = window.open(strUrl, strWindowName[, strWindowFeatures]);,这意味着 windowName 也应该传递给 window.open developer.mozilla.org/En/Window.open
    • 要在选项卡中打开弹出窗口,您应该设置弹出窗口在浏览器中的处理方式。例如:在 IE8+ 中,转到 Internet 选项、常规、选项卡部分,在遇到弹出窗口时设置部分
    【解决方案4】:

    这就是我最终使用的。暂时将 target 设置为 _blank,然后将其设置回来。

    OnClientClick="var originalTarget = document.forms[0].target; document.forms[0].target = '_blank'; setTimeout(function () { document.forms[0].target = originalTarget; }, 3000);"
    

    【讨论】:

      【解决方案5】:

      您必须在标题中添加以下内容:

      <script type="text/javascript">
              function fixform() {
                  if (opener.document.getElementById("aspnetForm").target != "_blank") return;
      
                  opener.document.getElementById("aspnetForm").target = "";
                  opener.document.getElementById("aspnetForm").action = opener.location.href;
                  }
      </script>
      

      然后在加载页面时调用fixform()

      【讨论】:

        【解决方案6】:

        您可以使用 Rout 重定向。

        protected void btnNewEntry_Click(object sender, EventArgs e)
        {
            Response.RedirectToRoute("CMS_1"); 
        }
        

        这需要在 Global.asax 文件中定义你的路由逻辑,可能是这样的:

        routes.MapPageRoute("CMS_1", "CMS_1", "~/CMS_1.aspx");
        

        CMS_1 模式在应用程序范围内的任何请求都将重定向到 CMS_1.aspx,但在 URL 中显示为 www.yoursite.com/CMS_1

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-05-25
          • 2022-07-30
          • 2016-03-11
          相关资源
          最近更新 更多