how to open a page in new tab on button click in asp.net?

前端 未结 16 1881
情书的邮戳
情书的邮戳 2020-11-29 07:32

I want to open a page in new tab of browser on button click.

I have searched a lot on google but i couldn\'t find anything.

Here is my button.



        
相关标签:
16条回答
  • 2020-11-29 07:47
    Add this Script  
    <script type = "text/javascript">
     function SetTarget() {
         document.forms[0].target = "_blank";
     }
    </script>
    and 
    <asp:Button ID="BTNpRINT"  runat="server" Text="PRINT"  CssClass="btn btn-primary"  OnClick="BTNpRINT_Click" OnClientClick = "SetTarget();"/>    
    and 
    protected void BTNpRINT_Click(object sender, EventArgs e)
        {
            Response.Redirect(string.Format("~/Print.aspx?ID={0}",txtInv.Text));
        }
    
    0 讨论(0)
  • 2020-11-29 07:49

    You have to use Javascript since code behind is server side only. I am pretty sure that this works.

    <asp:Button ID="btnNewEntry" runat="Server" CssClass="button" Text="New Entry" OnClick="btnNewEntry_Click" OnClientClick="aspnetForm.target ='_blank';"/>
    
    protected void btnNewEntry_Click(object sender, EventArgs e)
    {
      Response.Redirect("New.aspx");
    }
    
    0 讨论(0)
  • 2020-11-29 07:50

    You shuld do it by client side. you can place a html hyperlink with target="_blank" and style="display:none". after that create a javascript function like following

    function openwindow(){
    $("#hyperlinkid").click();
    return false;
    }
    

    use this function as onclientclick event handler of the button like onclientclick="return openwindow()" You need to include a jquery in the page.

    0 讨论(0)
  • 2020-11-29 07:52

    You can add to your button OnClientClick like so:

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

    This will change the current form's target for all buttons to open in new tab. So to complete the fix you can then use 2 approaches:

    1. For any other button in this form, add to client click a "reset form target" function like so:
    function ResetTarget() {
       window.document.forms[0].target = '';
    }
    
    1. Add the same code inside the function inside a setTimeout() so the code will reset the form's target after few moments. See this answer https://stackoverflow.com/a/40682253/8445364
    0 讨论(0)
  • 2020-11-29 07:53

    add target='_blank' after check validation :

        <asp:button id="_ButPrint"  ValidationGroup="print" OnClientClick="if (Page_ClientValidate()){$('form').attr('target','_blank');}"  runat="server" onclick="ButPrint_Click" Text="print" />
                                                                     

    0 讨论(0)
  • 2020-11-29 07:55

    Per Open a URL in a new tab (and not a new window) using JavaScript

    Nothing an author can do can choose to open in a new tab instead of a new window.

    The browser decides between opening a new tab or opening a new window. You cannot control this as a developer.

    0 讨论(0)
提交回复
热议问题