简体   繁体   中英

In ASP.Net, how can I open a link in a new window?

I have a button which redirects the user to another page. Instead, I would like this button to open a new window that points to this location. Can anyone please help me to do this?

aspx:

<asp:ImageButton ID="img_url"
                 CommandName='<%#Eval("url") %>'
                 OnClick="img_url_Click"
                 runat="server"
                 ImageUrl="~/images/products_details.png"
                 />

cs:

protected void img_url_Click(object sender, ImageClickEventArgs e)
{
    ImageButton img = sender as ImageButton;
    Response.Redirect(img.CommandName.ToString());
}

Just use a standard HTML anchor instead, containing the image.

<a href='<%# Eval("url") %>' target="_blank">
   <asp:Image RunAt="server"
              ID="detailsImage" 
              ImageUrl="~/images/products_details.png" />
</a>

You can add OnClientClick="formname.target='_blank'" to you ASP.net Controller.

<asp:ImageButton ID="img_url"
                 CommandName='<%#Eval("url") %>'
                 OnClick="img_url_Click"
                 runat="server"
                 ImageUrl="~/images/products_details.png"
                 OnClientClick="formname.target='_blank'"
                 />

Your codebehind redirect will occur on your new page.

Instead of onclick you could use onclientclick and pass the javascript window.open

something like:

<asp:ImageButton ID="img_url"
                 CommandName='<%#Eval("url") %>'
                 OnClientClick="window.open('<%#Eval("url") %>')"
                 runat="server"
                 ImageUrl="~/images/products_details.png"
                 />

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