简体   繁体   中英

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

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. I want to do it using LinkButton and I don't know how to do it by using the link button. 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. Take CommandArgument as a parameter to this method.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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