JavaTheScript
JavaTheScript

Reputation: 137

Opening URL using an ImageButton

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 { }
    }

Upvotes: 3

Views: 2891

Answers (2)

live-love
live-love

Reputation: 52376

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());" />

Upvotes: 0

wilso132
wilso132

Reputation: 829

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.

Upvotes: 3

Related Questions