如何使用链接按钮在新选项卡中打开链接

时间:2022-05-04 21:16:44

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

我有一个带有HeaderTemplate的gridview,它包含一个LinkBut​​ton。当我单击按钮时,我想在新选项卡中打开一个链接

<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

OnClick事件是

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.,.

这是在新标签中打开链接。但是我在gridview之外还有其他的LinkBut​​ton用于其他进程,例如保存数据等。

<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

但是,当我甚至单击此按钮时,它会打开新选项卡。如何阻止它们在新选项卡中打开

2 个解决方案

#1


0  

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.

当您设置ASP表单的目标时,每个链接或表单帖子都将以新窗口为目标。因此,您将不得不在每个其他LinkBut​​ton点击或表单提交上撤消表单的目标。

#2


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.

链接按钮是超链接样式按钮,具有与按钮相同的所有属性,但它仅以超链接样式显示。如果要在单击控件时链接到其他Web页面,请考虑使用HyperLink控件。

try:

Edit 2:

you are using like this..

你这样使用..

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

in a gridview or something like that..

在gridview或类似的东西..

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

然后你想在新窗口中导航所有超链接的URL。

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()

编辑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>

#1


0  

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.

当您设置ASP表单的目标时,每个链接或表单帖子都将以新窗口为目标。因此,您将不得不在每个其他LinkBut​​ton点击或表单提交上撤消表单的目标。

#2


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.

链接按钮是超链接样式按钮,具有与按钮相同的所有属性,但它仅以超链接样式显示。如果要在单击控件时链接到其他Web页面,请考虑使用HyperLink控件。

try:

Edit 2:

you are using like this..

你这样使用..

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

in a gridview or something like that..

在gridview或类似的东西..

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

然后你想在新窗口中导航所有超链接的URL。

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()

编辑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>