0

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

3 Answers 3

5

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>
1

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.

2
  • Nice trick, just keep in mind that from then on all other submit buttons on the previous window will open in a new window. Feb 27, 2013 at 20:48
  • :) thanks, that's true, but you can take it a step further by resetting the target once clicked. or something like that. Feb 28, 2013 at 7:58
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.