Open asp LinkButton in a new tab

家住魔仙堡 提交于 2019-12-23 04:21:34

问题


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());
}

回答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 > 



回答2:


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.



来源:https://stackoverflow.com/questions/43549221/open-asp-linkbutton-in-a-new-tab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!