Redirecting new tab on button click.(Response.Redirect) in asp.net C#

前端 未结 6 1013
花落未央
花落未央 2020-12-11 08:50

I\'m trying to open a page in new tab/window on button click.I tried in the google got this code but its not working.

Can anybody help me with this?

         


        
相关标签:
6条回答
  • 2020-12-11 09:17

    Wouldn't you be better off with

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

    Because, to replicate your desired behavior on an asp:Button, you have to call window.open on the OnClientClick event of the button which looks a lot less cleaner than the above solution. Plus asp:HyperLink is there to handle scenarios like this.

    If you want to replicate this using an asp:Button, do this.

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

    JavaScript function.

    var windowObjectReference;
    
    function openRequestedPopup() {
        windowObjectReference = window.open("CMS_1.aspx",
                  "DescriptiveWindowName",
                  "menubar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes");
    }
    
    0 讨论(0)
  • 2020-12-11 09:18

    I think your code should work just remove one thing from here but it'll do redirection from current page within existing window

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

    And if u wanna do the this via client side scripting Use this way

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

    According to me you should prefer the Client Side Scripting because just to open a new window server side will take a post back and that will be useless..

    0 讨论(0)
  • 2020-12-11 09:21

    You can do something like this :

    <asp:Button ID="Button1" runat="server" Text="Button"
        onclick="Button1_Click" OnClientClick="document.forms[0].target = '_blank';" />
    
    0 讨论(0)
  • 2020-12-11 09:28

    You can use Rout redirecting.

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

    which requires to define your routing logic in Global.asax file that could be like that:

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

    where any request by CMS_1 pattern in application scope will be redirecting to CMS_1.aspx, but in URL shows like www.yoursite.com/CMS_1

    0 讨论(0)
  • 2020-12-11 09:30

    You have to add following in header:

    <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>
    

    Then call fixform() in load your page.

    0 讨论(0)
  • 2020-12-11 09:34

    This is what I ended up using. Temporarily sets target to _blank, then sets it back.

    OnClientClick="var originalTarget = document.forms[0].target; document.forms[0].target = '_blank'; setTimeout(function () { document.forms[0].target = originalTarget; }, 3000);"
    
    0 讨论(0)
提交回复
热议问题