• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2496
  • Last Modified:

How to Redirect a Page on a Button or Image Button OnClick Event

I am new to web developing and I am attempting to redirect my page to another page on a website I am working on. I believe I may need to have to use JavaScript, but how would I go about redirecting in a button OnClick event to a web page called main.aspx. Any and all help would be greatly appreciated
3 Solutions
Paul MacDonaldDirector, Information SystemsCommented:
The obvious solution is to use an anchor (<a>) tag to link to the other page.  If you have to (or just want to) do something in code, Response.Redirect can be used in the code behind.

Code (including JavaScript) is fine, but unneccessary unless you're doing something more than sending the person to another page.
Use the location object like so:-

<input type="button"  onclick="window.location.replace('http://www.google.com')" >
thenthorn1010Author Commented:

I have tried using a Response.Redirect in both the code behind and JavaScript and cannot get the ASP ImageButton to redirect to the main.aspx page. Is it wrong to put Response.Redirect(main.aspx); in the JavaScript both inline and in the same asp .net file with the image button and the code behind to redirect to this page? I keep getting compiler errors stating that I am missing semicolons or the expression is too long for the literal. I have tried both ways and ended up with similar results. Any ideas on another way to have a button appear as with an image on it and then redirect?
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Paul MacDonaldDirector, Information SystemsCommented:
I think you're missing the "PostBackURL" property...
<asp:imagebutton ID="Imagebutton1" runat="server" PostBackUrl="main.aspx"></asp:imagebutton>
Alan WarrenCommented:
Hi thenthorn1010,

You could automate the redirection with meta tag in the head.
The content property designates how many seconds to wait before redirection.

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <meta id="Meta1" http-equiv="refresh" content="2;url=main.aspx" runat="server" />
    <form id="form1" runat="server">

Open in new window

Aspx code.
<asp:ImageButton ID="imgBtn" runat="server" ImageUrl="image.jpg" 
            onclick="imgBtn_Click" />

Open in new window

Code Behind code.

protected void imgBtn_Click(object sender, ImageClickEventArgs e)
            Response.Redirect("Main.aspx", false);

Open in new window

Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
I keep getting compiler errors stating that I am missing semicolons or the expression is too long for the literal

And you said you are using Response.Redirect too for redirecting. In that case I would like to see your code part so that your mistakes can be rectified.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now