2015-09-04 74 views
2

我有gridviewHeaderTemplate,它包含LinkButton。當我點擊按鈕,我想在new tab如何使用鏈接按鈕打開新選項卡中的鏈接

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

JavaScript

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

打開一個鏈接,OnClick事件是

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

這是打開新標籤的鏈接。 但是我有其他LinkButton以外的gridview等其他進程如Saving的數據等。

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

但是,當我連點擊這個按鈕,它也會打開新的tabs.How我能阻止他們在新標籤頁

+0

添加'OnClientClick'到'LinkBut​​ton1'即撤銷什麼'SetTarget'一樣。 – dman2306

回答

0

有你爲什麼要在服務器端重定向的理由開放?你可以只是做

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

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

當你設置ASP形式的目標,每一個環節或郵寄的形式將瞄準新的窗口。所以你將不得不在每一次LinkBut​​ton點擊或表單提交時撤消表單的目標。

+0

被重定向動態與一些manipulations.So加載的鏈接要在服務器端 –

0

拳的是,見This Link

重要信息

鏈接按鈕是超鏈接式按鈕,都財產一樣 按鈕,但只出現在一個超鏈接樣式。如果您想在單擊控件時鏈接到另一個網頁,請考慮使用 HyperLink控件。

嘗試:

編輯2:

你正在使用這樣的..

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

gridview或類似的東西..

,然後您想要在新窗口中導航所有超鏈接的URL。

因此,您可以動態設置網址。

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

編輯1: JavaScriptwindow.open()

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

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

我需要一些服務器端的操作,我剛纔給樣本link.The鏈接是動態加載的 –

+0

所以,你不能這樣做這種方式.. 你需要遵循'給定鏈接'的解決方案。 你會得到你需要的一切。根據您的要求。 –

+0

超鏈接沒有'OnClick'事件 –

相關問題