ha chitti
ha chitti

Reputation: 139

How to open link in new tab with Link Button

I have a gridview with the HeaderTemplate and it contains a LinkButton. When I click the button I want to open a link in new tab

<asp:TemplateField>
     <HeaderTemplate>
      <asp:LinkButton ID="lnkbtn" runat="server" OnClientClick="SetTarget();" OnClick="lbtn_Click">Topics</asp:LinkButton>
     </HeaderTemplate>
<ItemTemplate>
........
</ItemTemplate>

My JavaScript

<script type = "text/javascript">
        function SetTarget() {
            document.forms[0].target = "_blank";
        }
</script>

And the OnClick event is

protected void lbtn_Click(object sender, EventArgs e)
{
  Response.Redirect("http://www.google.com/");
}

This is opening the link in new tab. But I have other LinkButton outside the gridview for other processes such as Saving the data, etc.,.

<asp:LinkButton ID="LinkButton1" runat="server">Save</asp:LinkButton>

But when I even click this button it is opening new tabs.How can I prevent them from opening in new tabs

Upvotes: 2

Views: 5016

Answers (3)

Kevin Swann
Kevin Swann

Reputation: 1038

When your OnClientClick JavaScript sets the form target such as this

<script type = "text/javascript">
        function SetTarget() {
            document.forms[0].target = "_blank";
        }
</script>

you are setting the target for every Button and LinkButton on that page. All buttons will then open in a new tab which may not be what you want. This is because as the target is being set for the (one) page form.

Using ASP.NET web forms, for each web page, there is only one page form. Using the browser > view source

...
<body>
  <form method="post" action="./SearchIndex.aspx" id="form1">
...

If you use this method, is better to do this

<script type="text/javascript">
    function SetTarget() {
        window.document.forms[0].target = '_blank'; 
        setTimeout(function () { window.document.forms[0].target = ''; }, 0);
    }
</script>

where the page form target is set for that button click (opens in new tab) then reset immediately after.

Upvotes: 0

A_Sk
A_Sk

Reputation: 4630

Fist of all, See This Link

important Info

a link button is a hyperlink-style button , have all property same as a button, but it only appears in a hyperlink style. If you want to link to another Web page when the control is clicked, consider using the HyperLink control.

try:

Edit 2:

you are using like this..

<asp:LinkButton ID="LinkButton1" runat="server">Save</asp:LinkButton>

in a gridview or something like that..

and then you want to navigate a URL in new window for all the Hyperlink.

so, you can set the url dynamically.

Like

<asp:TemplateField HeaderText="Log" ItemStyle-Width="15%">
        <ItemTemplate>
            <asp:HyperLink runat="server" 
                NavigateUrl='<%# GetUrl(Eval("Base_Id"))%>' 
                text="Log" target="_blank"></asp:HyperLink>
        </ItemTemplate>
    </asp:TemplateField>

.cs

protected string GetUrl(object id)
{
return "http://somelink&RecordId=" + id;
}

Edit 1: JavaScript window.open()

javascript:window.open('xyz.aspx')

Like

<asp:LinkButton ID="lb1" runat="server" OnClientClick="javascript:window.open('xyz.aspx')">click me</asp:LinkButton>

Upvotes: 0

Victor Levin
Victor Levin

Reputation: 1177

Is there a reason why you want to redirect on the server-side? You could just do

function redirectToGoogle(){
   window.open('google.com');
   return false;
}

and

<asp:LinkButton ID="lnkbtn" runat="server" OnClientClick="return redirectToGoogle();" ....

When you're setting ASP form's target, every link or form post will target new window. So you're going to have to undo form's target on every other LinkButton click or form submission.

Upvotes: 1

Related Questions