tags:

views:

1075

answers:

5

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?

A: 

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.

TheTXI
Why does it work in firefox, but not IE?
Xaisoft
+2  A: 

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'" />
Mehrdad Afshari
Im just using the PostBackUrl property on the button which works fine.
Xaisoft
PostBackUrl is another story but it should work too. There are just too many solutions to this problem.
Mehrdad Afshari
Any idea why my code above would not work in IE, but in firefox.
Xaisoft
Probably firefox considers the click on the button also for the hyperlink. Not sure though :)
Mehrdad Afshari
+1  A: 

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)?

bendewey
Actually, I think I did this by accident, but now I am curious as to why it works in firefox, but not IE. This may not be the best answer, but I am using a button because I am setting a CssClass on the button.
Xaisoft
A: 

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

Nick Allen - Tungle139
That is exactly what I am doing with the button, but I was just curious as to why it works in firefox and not in ie.
Xaisoft
A: 

Xaisoft i was also stuck with the same problem as you have described....this was due to the reason that my internet explorer browser cookies was disabled....so internet explorer would not redirect to any page....hope this is the reason you were finding....thanks...bye...take care...

abhishek