3

using asp.net | C#

I want my ImageButton to open a URL when I click it. I finally have my Image loading and it clicks but when I click it nothing happens. Here is the code I have so far:

aspx page

   <asp:ImageButton ID="Button1" runat="server" ImageUrl="~/images/button.gif" 
    onclick="Open_Click"></asp:ImageButton>

aspx.cs page

    protected void Open_Click(object sender, System.Web.UI.ImageClickEventArgs e)
    {
        try
        {
            System.Diagnostics.Process.Start("http://www.website.com");
        }
        catch { }
    }

2 Answers 2

3

You want to do a redirect, not start a process. Try this:

protected void Open_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
    try
    {
        Response.Redirect("http://www.website.com");
    }
    catch { }
}

Additionally, you could just set the PostBackUrl attribute on the control and have no need for a server side event.

Sign up to request clarification or add additional context in comments.

Comments

0

You can do it on the client-side:

This will open in another window:

<asp:ImageButton OnClientClick="window.open('/xxx/xxx.aspx');

OR this will open in same window, javascript needs to return false so server code won't run:

    <script>
        function ReDirect() {
            location.href = '/xxx/xxx.aspx';
            return false;
        }
    </script>

asp:ImageButton OnClientClick="javascript:return(ReDirect());" />

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.