A New Internet Library: Add Your Website/Blog or Suggest A Website/Blog to our Free Web Directory http://anil.myfunda.net.

Its very simple, free and SEO Friendly.
Submit Now....

Monday, February 25, 2008

Problem with Submitting a Form with the Enter Key and a Button Web Control's Click Event

If your site has a Web Form with a single TextBox Web control your user's visiting with Internet Explorer won't be able to have the appropriate action be completed upon hitting enter in the textbox. That is, they'll hit Enter and the form will postback, but whatever custom logic you had written in the Button's Click event handler won't run, thereby making it seem to the user that nothing happened.

To overcome this problem you can use the following workaround - simply add another TextBox Web control to the Web Form. This will cause Internet Explorer to send back the Button Web control's name/value pair upon hitting Enter. Of course, you don't want the user to see two TextBoxes, so use a bit of CSS to hide the second one. That is, instead of using this:

<form runat="server">    Name: <asp:TextBox runat="server" id="txtName" ... />    <br />    <asp:Button runat="server" Text="Click Me!" ... /> </form>         

Add an additional TextBox Web control to overcome the Enter problem in IE, but hide the TextBox so your users see only one:

<form runat="server">    Name: <asp:TextBox runat="server" id="txtName" ... />    <br />    <asp:TextBox runat="server" style="visibility:hidden;display:none;" />    <asp:Button runat="server" Text="Click Me!" ... /> </form>         

That's all there is to it!

Dotnet-Interviews