how to use image button to open the url in another window

Mr A picture Mr A · Apr 21, 2011 · Viewed 62k times · Source

Hi I know how to acheive this in hyperlink by setting target = _blank , how can i do this using image button control , below is my code:

<asp:ImageButton OnClick="test_Click" ImageUrl="/images/contactUs/directionbtn.png" ID="test" runat="server" ValidationGroup="group2" />

             <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
            ControlToValidate="txtPostcode1" ErrorMessage="Postcode is required"  ValidationGroup="group2"></asp:RequiredFieldValidator>
            <br />

Code behind:

    protected void test_Click(object sender, ImageClickEventArgs e)
{

    Response.Redirect(String.Format("http://maps.google.co.uk/maps?saddr={0}&daddr=&daddr=Wigan+WN6+0HS,+United+Kingdom&iwloc=1&dq=Tangent+Design", txtPostcode1.Text));



}

Any help or advice will be highly appreciated

Answer

Jack Marchetti picture Jack Marchetti · Apr 21, 2011
protected void Page_Load() {
   ControlID.Attributes.Add("target", "_blank");    
}

If that doesn't work, try adding this to your ImageButton:

<asp:ImageButton runat="server" OnClientClick="window.open('http://url/to/open');" ></asp:ImageButton>