find a location for property in a new city

Monday, 14 January 2013

Web forms button click event not firing in IE

There are probably many reasons why a button would not fire in an ASP.NET web forms application. Some can be fixed with deleting cookies and history, some are to do with when the event is registered. I found a new one though.

In my scenario I had a text box that was submitted with a button next to it. We didn't want to display the button though but instead wanted the form submitted with a click of the Enter button. The button was therefore wrapped in a display:none span, like this:

<input type="text" id="txtInput" onchange="btnGo_Click" runat="server"/>
<asp:Button style="display:none;" runat="server" OnClick="btnGo_Click" />
<asp:Label ID="lblOutput" runat="server" />

It turns out that this worked just fine in Chrome and Firefox but not in IE9. This was because not all browsers post data that is hidden with CSS. Security possibly? You could see this by looking at the request in Fiddler. There was no value for the button field in IE but there was for the others.

How to submit a form by clicking Enter and hiding the button

The best way I can think of is to hide the button without hiding it... How? You can push it miles of the page to hide the button with some CSS like this:

<input type="text" id="txtInput" onchange="btnGo_Click" runat="server"/>
<asp:Button style="position:absolute;left:-9999px;" runat="server" OnClick="btnGo_Click" />
<asp:Label ID="lblOutput" runat="server" />

This will put the button miles off to the left never to be seen by the user.

Follow britishdev on Twitter

4 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. I love to read this type of material Good and attractive information I take from it..Thank you for posting such a good article
    happy wheels ez
    super mario bros
    pacman
    agario

    ReplyDelete
  3. I really like this kind of post here. I want to share more post about that topic. keep it up. Grass mats from groundreinforcementmesh.co.uk from http://groundreinforcementmesh.co.uk/grass-mats.html

    ReplyDelete
  4. The blog is Really informative and looks realistic. I appreciate your style and surely come back again to increase my knowledge.Mostly comments to match to eatch other.I just got to this amazing site not long ago. I was actually captured with the piece of resources you have got Garage Flooring

    ReplyDelete