2

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

ha chitti
  • 139
  • 2
  • 11

3 Answers3

1

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.

Victor Levin
  • 1,167
  • 6
  • 11
0

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>
Community
  • 1
  • 1
A_Sk
  • 4,532
  • 3
  • 27
  • 51
  • I have to some server side manipulations, I have just given a sample link.The link is dynamically loaded – ha chitti Sep 04 '15 at 16:06
  • so, you can't do this way.. you need to follow the `given link's` solution. you'll get everything you need. as per your requirement. – A_Sk Sep 04 '15 at 16:19
  • hyperlink doesn't have `OnClick` event – ha chitti Sep 04 '15 at 16:46
  • Hyperlink doesn't need `onClick`. It's got `NavigateURL`. It appears you're doing this the wrong way. If you load data into GridView, you should output the link into `HyperLink` the same way you would do a `Label`. If link needs manipulation, use grid's `_ItemDataBound` sub to do so. As grid iterates through data `ItemDataBound` will be called on every data-bound control, including `HyperLink`... You can retrieve NavigateURL, manipulate it and stuff it back in. – Victor Levin Sep 04 '15 at 18:00
  • @user3540365 The id is always null in `cs` – ha chitti Sep 05 '15 at 01:39
  • @hachitti can't you format it as per your requirement?? then you really need to start from beginning. – A_Sk Sep 05 '15 at 11:21
0

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.

Kevin Swann
  • 1,018
  • 12
  • 28