IT_info
IT_info

Reputation: 727

On click event of image button

I have a problem that when I click on the image button instead of redirect me to the corresponding page, it will just redirect back to the same page.

This is the code in asp;

  <asp:ImageButton ID="header1" src="Resources/Icons/Header1.jpg" runat="server" />

And this is in my page load in the code behind;

  header1.Attributes.Add("onclick", "~/ChildSelection.aspx");

Any ideas why this is happening?

Upvotes: 2

Views: 24652

Answers (4)

SurinderBhomra
SurinderBhomra

Reputation: 2199

Your Image button should have an onclick event.

<asp:ImageButton ID="header1" ImageUrl="Resources/Icons/Header1.jpg" runat="server" OnClick="header1_Click" />

protected void header1_Click(object sender, ImageClickEventArgs e)
{
    Response.Redirect("~/ChildSelection.aspx");
}

Upvotes: 2

Apurv Gupta
Apurv Gupta

Reputation: 860

I guess you can achieve it by replacing it by

header1.Attributes.Add("PostBackUrl", "~/ChildSelection.aspx");

Onclick is an event. You might also consider using a hyperlink with ImageSrc property instead of an ImageButton.

[Edit]

If you are just trying to redirect a better approach could be

<asp:HyperLink ID="header1" runat="server" ImageUrl="Resources/Icons/Header1.jpg">Click Here</asp:HyperLink>

And in code behind

header1.NavigateUrl = "~/ChildSelection.aspx";

Upvotes: 1

Brian
Brian

Reputation: 1823

The onclick event executes javascript if I'm not mistaking. OnClick refers to a server-side event. Try setting the window's location to redirect to the corresponding page.

 header1.Attributes.Add("onclick", 
   string.Format("window.location = '{0}'", ResolveClientUrl("~/ChildSelection.aspx")));

Upvotes: 1

ankit rajput
ankit rajput

Reputation: 182

Use the following code:

<asp:ImageButton ID="header1" src="Resources/Icons/Header1.jpg" runat="server" PostBackURL="~/ChildSelection.aspx"/>

Upvotes: -2

Related Questions