Xaisoft
Xaisoft

Reputation: 46591

asp:Button inside asp:hyperlink does not navigate to page in internet explorer

I have an asp:button which is inside an asp:hyperlink. When you click the button in firefox, it goes to the correct url specified by the asp:hyperlink, but if you click the button in internet explorer, it stays on the same page. I am just using the PostBackUrl property on the button to resolve the issue, but here is an example of the code:

<asp:Hyperlink ID="hyp" runat="server" NavigateUrl="Page2.aspx">
<asp:Button ID="btn" runat="server" Text="Submit" /></asp:Hyperlink>

Why does the above work in firefox, but not IE?

Upvotes: 1

Views: 11858

Answers (4)

bendewey
bendewey

Reputation: 40235

Is there a reason why you are using a button inside a hyperlink? Depending on the design you are trying to achive I would use just a Button or a LinkButton and then do a redirect after your logic in the codebehind

<asp:Button runat='server' id='button1' Text='Click Me' Click='button1_Click' />
<asp:LinkButton runat='server' id='linkbutton1' Text='Click Me'  Click='button1_Click' />

Code-Behind

protected void button1_Click(object sender, EventArgs e) {
  // some logic
  Response.Redirect("Page2.aspx");
}

Firefox vs Internet Explorer

I suspect your having discrepencies between Firefox and Internet Explorer because of the way the events are bubbled/propogated between the browsers. If you would like to cancel the propagation of the event, you would need to include a call to event.preventDefault() or event.stopPropagation() in your button click event handler (in javascript)?

Upvotes: 1

Nick Allen
Nick Allen

Reputation: 12220

I would use an <asp:LinkButton CssClass="myButton" OnClick="Redirect" /> and then CSS to style it how you want it to look and the code-behind to handle the functionality.

protected void Redirect(object sender, EventArgs e)
{
   // do something
}

Three way seperation of presentation, markup and functionality is the way to go in my opinion

Upvotes: 0

Mehrdad Afshari
Mehrdad Afshari

Reputation: 421978

What you did is not very correct.

Just add the button and in its click handler do:

Response.Redirect("Page2.aspx");

Alternatively you can write a line of javascript:

<input type="button" value="Text" onclick="location='Page2.aspx'" />

Upvotes: 7

TheTXI
TheTXI

Reputation: 37885

Your button is executing a postback on the same page. If I were you I would try and use a standard button in your HTML toolbox instead of an ASP.NET button.

Edit: I would more highly suggest eliminating your button control and styling your hyperlink appropriately, or eliminate both and use a hyperlinkbutton control instead and have its display properties set to display as a button.

Upvotes: 0

Related Questions