Search code examples
c#asp.netasplinkbutton

Open asp LinkButton in a new tab


I have a button in my gridview and when the users click on it, it goes to the page. However, if you right click on it and "open link in a new tab" it goes to a blank page. I want it so when the user right click on it and "open link in a new tab" to go to the page. This is the code that I have so far:

aspx

 <asp:LinkButton ID="lnkEditbtn" data-toggle="tooltip" title="View Request" OnClick="lnkEditbtn_Click" runat="server" class="btn btn-primary btn-sm" Text="<%# bind('ticketID')%>"></asp:LinkButton>    

c#

protected void lnkEditbtn_Click(object sender, EventArgs e)
{
    GridViewRow gvr = (GridViewRow)(((Control)sender).NamingContainer);
    Label lblid = (Label)gvr.FindControl("lblMovie");
    int id = Convert.ToInt32(lblid.Text.ToString());
    SecureQueryString qs = new SecureQueryString();
    qs["ID"] = id.ToString();
    Response.Redirect("viewMovie.aspx?qs=" + qs.ToString());
}

Solution

  • you cannot do this with linkbutton because it redirects to the desired view after you click on it but you can use asp:HyperLink and set its value like

    <asp:HyperLink ID="lnkEditbtn" data-toggle="tooltip" Text="View Request"  runat="server" NavigateUrl='<%# Eval("ticketID", "~/viewMovie.aspx?qs={0}")  %>' class="btn btn-primary btn-sm" ></asp:HyperLink > 
    

    Edit

    if you want the URL to be encrypted first create a class

     public static class encrypt
        {
            public static string encvalue(int id)
            {
                SecureQueryString qs = new SecureQueryString();
                qs["ID"] = id.ToString();
                return  qs.ToString()
            }
        }
    

    and your hyperlink will be

    <asp:HyperLink ID="lnkEditbtn" data-toggle="tooltip" Text="View Request"  runat="server" NavigateUrl='<%# String.Format("~/viewMovie.aspx?qs={0}",encrypt.encvalue(Convert.ToInt32(Eval("ticketID"))))  %>' class="btn btn-primary btn-sm" ></asp:HyperLink >