3

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

WhiteKnight
  • 4,938
  • 5
  • 37
  • 41
Mr A
  • 6,448
  • 25
  • 83
  • 137
  • 2
    Use an anchor as a container of the image. Or asp.net jargon, – Roberto Alarcon Apr 21 '11 at 15:54
  • currently the link opens ont he same page – Mr A Apr 21 '11 at 15:54
  • i need to pass txtpostcode value to the link , how will i acheive that then4 – Mr A Apr 21 '11 at 15:56
  • ok... do it in javascript in javascript: foo(){ var txtPostCode = document.getElementById('txtPostcode1').value; window.open("http://maps.google.co.uk/maps?saddr=" + txtPostcode1 + "&daddr=&daddr=Wigan+WN6+0HS,+United+Kingdom&iwloc=1&dq=Tangent+Design"","MyWindow","height=375,width=350"); } // don't forget to validate if(txtPostcode1 !== ''){foo();} – Roberto Alarcon Apr 21 '11 at 16:00
  • 1
    using script the validation group will not work – Mr A Apr 21 '11 at 16:06

7 Answers7

6
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>
ag93
  • 343
  • 4
  • 17
Jack Marchetti
  • 15,536
  • 14
  • 81
  • 117
4

I just figure it out..

On Page_Load event, put

    this.Form.Target = "_blank"; // Will set all link's target to a new window

Then for example in a image button Click event, you put:

    Response.Redirect("http://stackoverflow.com");

It will simply open this page in a new tab. Try it :)

ahmadnaziF
  • 149
  • 3
  • 8
3

Add target="_blank" to onClientClick will do the trick

3

In the code behind.

imgbtn.OnClientClick = "target='blank'";

And you're done.

M H
  • 2,179
  • 25
  • 50
3

you could use the Attributes collection to add "target","_blank"

this should add the target attribute to the anchor link surrounding the image

Sonic Soul
  • 23,855
  • 37
  • 130
  • 196
  • do we have to do something like this ImageButton1.Attributes.Add("target", "_blank"); – Mr A Apr 21 '11 at 16:02
0

Try this:

<asp:ImageButton OnClick="test_Click" ImageUrl="/images/contactUs/directionbtn.png" ID="test" runat="server" ValidationGroup="group2" OnClientClick="form1.target ='_blank';" />
0
this.Form.Target = "_blank"; 

This way the client can see what he wants in a new page, since the server and what is available and his account in the site available at the beginning PageLoad

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109