简体   繁体   English

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

[英]How to open a link in a new tab using ASP Link Button Property?

I have a gridview with the Template and it contains a LinkButton. 我有一个带有Template的gridview,它包含一个LinkBut​​ton。 When I click the button I want to open a link in new tab 当我单击按钮时,我想在新标签页中打开链接

 <Templates>
 <Obout:GridTemplate runat="server" ID="tempCurrTask">
     <Template>
         <asp:LinkButton Text='<%# Container.DataItem["CurrentTask"] %>' ID="lnkbtnview2"
                runat="server" Font-Underline="true" OnCommand="SELREC" CommandArgument='<%# Container.PageRecordIndex %>'></asp:LinkButton>
     </Template>
</Obout:GridTemplate>

And the SELREC function is SELREC功能是

protected void SELREC(object sender, CommandEventArgs e)
{

        int rowIndex = int.Parse(e.CommandArgument.ToString());
        Hashtable dataItem = grvLeads.Rows[rowIndex].ToHashtable() as Hashtable;
        string id = Convert.ToString(dataItem["iTask_id"]); //.Split('|');
        string rowIndexid = id.ToString();
            //+ "/" + e.CommandName.ToString();
        //ScriptManager.RegisterStartupScript(this, typeof(string), "openWindow", "window.open('Task.aspx?TaskID=" + rowIndexid.Trim() + "', '_newtab','left = 10, top=10,scrollbars=Yes,resizable=yes,width=1100,height=580'); ", true);
        Response.Redirect("Task.aspx?TaskID=" + rowIndexid.Trim());

}

This link opens in the same tab. 该链接在同一选项卡中打开。 I want it to open in new tab, So I changed the asp:LinkButton to asp:HyperLink tag but the SELREC function is not called properly. 我希望它在新选项卡中打开,因此我将asp:LinkBut​​ton更改为asp:HyperLink标记,但是未正确调用SELREC函数。 I want to do it using LinkButton and I don't know how to do it by using the link button. 我想使用LinkBut​​ton来做到这一点,但我不知道如何使用链接按钮来做到这一点。 So please anybody help me with sample code. 因此,请任何人帮助我提供示例代码。

Try this approach; 试试这种方法;

<asp:LinkButton runat="server" href='<%# "Task.aspx?TaskID=" + MethodtoGenerateTaskId(parameter) %>'   target="_blank">LinkButton</asp:LinkButton>

You should define MethodtoGenerateTaskId(parameter) in c# codebehind. 您应该在后面的c#代码中定义MethodtoGenerateTaskId(parameter)。 Take CommandArgument as a parameter to this method. 将CommandArgument作为此方法的参数。

protected string MethodtoGenerateTaskId(string command_arg)
 {

  int rowIndex = int.Parse(command_arg.ToString());
    Hashtable dataItem = grvLeads.Rows[rowIndex].ToHashtable() as Hashtable;
    string id = Convert.ToString(dataItem["iTask_id"]); //.Split('|');
    string rowIndexid = id.ToString();

   return rowIndexid.Trim();
  }

and in markup; 和标记;

<asp:LinkButton runat="server" href='<%# "Task.aspx?TaskID=" +      MethodtoGenerateTaskId(Container.PageRecordIndex.ToString()) %>'    target="_blank">LinkButton</asp:LinkButton>

and if it works; 如果可行; pls mark it as answer... 请标记为答案...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM