Myworld
Myworld

Reputation: 1899

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

Upvotes: 0

Views: 13364

Answers (3)

tvanfosson
tvanfosson

Reputation: 532435

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>

Upvotes: 5

Marc Uberstein
Marc Uberstein

Reputation: 12541

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.

Upvotes: 1

Adam Butler
Adam Butler

Reputation: 3037

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"
                 />

Upvotes: 0

Related Questions