0

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());
}
Boney
  • 2,072
  • 3
  • 18
  • 23
Charles Xavier
  • 1,015
  • 3
  • 14
  • 33

2 Answers2

1

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 > 
Usman
  • 4,615
  • 2
  • 17
  • 33
  • This seems to do the job! But how do you encrypt the URL right now it shows the number of the movieID e.g 123 and not qs=IFIYPQFIEhU%3d. The onclick I have (Check original code): SecureQueryString qs = new SecureQueryString(); qs["ID"] = id.ToString(); Response.Redirect("viewMovie.aspx.aspx?qs=" + qs.ToString()); – Charles Xavier Apr 24 '17 at 13:44
  • It's suppose to go to aspx?qs=IFIYPQFIEhU%3d rather than .aspx?qs=261 – Charles Xavier Apr 24 '17 at 13:51
  • @ParbhuBissessar can you tell which library are you using for SecureQueryString ? – Usman Apr 24 '17 at 14:01
  • see if this helps using System.Collections.Specialized; using System.Security.Cryptography; – Charles Xavier Apr 24 '17 at 14:04
  • It's telling me now that The name 'id' does not exist in the current. contextqs["ID"] = id.ToString(); I've tried to put back the GridViewRow grv (as in the above code) but it says: The name 'sender' does not exist in the current context – Charles Xavier Apr 24 '17 at 15:37
  • 1
    @ParbhuBissessar my bad just change the parameter name from value To id in the class i have updated in my code – Usman Apr 24 '17 at 15:38
0

Link button in server side gets rendered to Hyperlink in client side with 'href' as href="javascript:__doPostBack('lnkEditbtn',''), which is nothing but a postback to the server from the link button. So when you right click and open the link in a new tab, it posts to the server and hence it comes up as blank page in new tab.

What you can do is use code similar to the below code:

<style>
    .hide {
        display:none;
    }
</style>
<script>        
    function postBack() {
        __doPostBack('lnkEditbtn', '');
        return false;
    }
</script>

<asp:LinkButton ID="lnkEditbtn" runat="server" OnClick="lnkEditbtn_Click" Text="Link" CssClass="hide"></asp:LinkButton>
<a href="http://www.google.com" onclick="return postBack();">Link</a>



protected void lnkEditbtn_Click(object sender, EventArgs e)
{
   var linkButton = (Control)sender as LinkButton;
}

With this code, you are hiding the link button and using the Anchor tag instead. Href in Anchor tag will be invoked when you right click. And when you click the link, "postBack" JS method will be triggered that invokes the server side event handler for the Link Button.
And there by both right click and left click works.

Boney
  • 2,072
  • 3
  • 18
  • 23
  • I dont want the link to go to google.com but want it to go to the page eg: viewMovie.aspx?qs= – Charles Xavier Apr 21 '17 at 20:52
  • The link is generated from the c# page and it's not a fixed link – Charles Xavier Apr 21 '17 at 21:00
  • 1
    ok got you. Would you be able to process the Href value, before the page renders in client side OR is there a chance the value of Href will change due to client interaction ? If it is the former case, then you dont need the LinkButton as such. Use a hyperlink, set Href in the server side before page renders and the hyperlink will respond to click and right click. – Boney Apr 22 '17 at 03:54