2

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.

Md Aslam
  • 1,228
  • 8
  • 27
  • 61

1 Answers1

0

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

oneNiceFriend
  • 6,691
  • 7
  • 29
  • 39
  • Where I want to define the " MethodtoGenerateTaskId(parameter) " ? In Java Script or C#?. The above given approach is working, but here the problem is, I cannot pass the Task ID. SO please help me with some code for defining the method. – Md Aslam Feb 17 '16 at 13:31