C# Target=“_blank” in a LinkButton

前端 未结 8 1331
无人及你
无人及你 2020-12-06 00:58

is it possible to have a target=\"_blank\" in LinkButton...mine doesnt seem to be working



        
相关标签:
8条回答
  • 2020-12-06 01:26

    Adding to @Devsainii answer above.

    Add the attribute OnClientClick="window.document.forms[0].target='_blank';" to your LinkButton and then in the code behind, just use Response.Redirect("~/AnotherPage.aspx") to navigate to another page.

    0 讨论(0)
  • 2020-12-06 01:32

    You can use the Hyperlink control, which does have a target='_blank' property. However if you must use a LinkButton control, then you can add a OnClientClick attribute which then calls a JavaScript function to open a popup window

    window.open();
    
    0 讨论(0)
  • 2020-12-06 01:34

    None of the current answers are correct, even the <a> tag is not the correct answer in asp.net.

    Use the HyperLink Button. There is even a property for the target attribute.

    <asp:HyperLink runat="server" 
        NavigateUrl='http://rrs.com/aspx/Equipment/EquipmentType.aspx'
        Target="_blank">
            Create/Edit Part Types
    </asp:HyperLink>
    
    0 讨论(0)
  • 2020-12-06 01:40

    You can use like this with Link Button

    Replace target="_blank" to OnClientClick="window.document.forms[0].target='_blank';".

    0 讨论(0)
  • 2020-12-06 01:42

    I was working with Devsaninii's answer where I changed the target of the form and found the undesired behavior of the rest of my pages switching to a new window after clicking a link that changed the target. Which made sense but was undesirable.

    I was opening files with some links and loading new pages with others. I wanted the files to open in new windows and I wanted the new pages to open in the same window. But after I changed the target everything was in a new window. I could have gone through and added a client click handler to each and every linkbutton, but that was too cumbersome.

    So here is what I came up with:

    I added a class to my linkbuttons that were supposed to have a new window as the target and then I added this little piece of jQuery to my script:

    $(function() {
        $('a').click(function() {
            if ($(this).hasClass('changeTarget')) {
                window.document.forms[0].target = '_blank';
            } else {
                window.document.forms[0].target = '_self';
            }
        });
    });
    

    Now when a linkbutton that should have a new window is pressed, it opens in a new window, and when anything else is pressed, it opens in the same window.

    0 讨论(0)
  • 2020-12-06 01:48

    A LinkButton in ASP.NET web forms just submits the underlying form using JavaScript so the target="_blank" doesn't actually do anything. To open a new window you can instead modify the target property of the "form" via JavaScript using the LinkButton's OnClientClick event. You also need to undo the modification after the click (using a setTimeout) otherwise further button clicks will unintentionally target a new tab.

    <asp:LinkButton ID="uiNewTabExample" Text="Open New Tab" OnClick="uiNewTabExample_Click" OnClientClick="window.document.forms[0].target = '_blank'; setTimeout(function () { window.document.forms[0].target = '' }, 0);" 
    runat="server" />
    
    0 讨论(0)
提交回复
热议问题