开发者

ASP.net Text Box Enter button pressed hell

Basically I have an asp.net website with login and search pages.

Currently, I have no idea why, when ever a user hits enter in either of the login text boxes (user name/password) or in the Seach text box, the website is redirected to the default page.

I have no idea why this is happening, I've tried setting defaultButton on both the panel containing the search and the 开发者_JAVA百科login panel but that doesnt seem to work.

I've also tried catching the key press event with javascript which isnt working either.

I have no idea what event is being fired and why, or why it seems to override everything I try to do.

Anyone seen anything like this before?

---------------Edit-------------------

 <div class="searchBar" onkeypress="javascript:return
  WebForm_FireDefaultButton(event,'ctl00_MainContent_logView1_LogRepeater_ctl02_lbSearch')">

  <input name="ctl00$MainContent$logView1$LogRepeater$ctl02$ctl02" type="text" />
  <a id="ctl00_MainContent_logView1_LogRepeater_ctl02_lbSearch" href="javascript:__doPostBack('ctl00$MainContent$logView1$LogRepeater$ctl02$lbSearch','')">
  Search</a>
 </div>

Ok so here is the code generated by ASP.net that is getting ignored.

---------------Update----------------

I have tried adding a hidden button and setting that button as the defaultbutton of the form and that has stoped it from redirecting to the default page each time but this does mean that pressing enter in a text box just reloads the page, which is better but still isnt Ideal.


This may not be an ASP.NET quirk as much as it is a web-browser quirk.

When you press enter in a single-line text field, the browser will submit the form to the action specified in the form tag.

ASP.NET "helpfully" creates a form tag just inside the body tag and puts all body elements inside it...

As far as I know, you can safely remove this form tag; the HTML specification allows fields outside of form tags for use in JavaScript/AJAX operations.


I have seen this behaviour before, it occurred when the first button on the form was the LoginControl. Everytime enter was pressed it would fire the click event for the login and essentially log the user out. Are you using the LoginControl?

In the end I had to add a hidden button before the login control. The hidden button didn't do anything but it prevented the LoginControl from executing.


I had this same problem once. We had a login form that worked fine when a user hit enter. Then at one point we added a search box/button in the menu bar at the top of the page. Suddenly when users would hit enter they would attempt to perform a search. I resolved it by setting the .DefaultButton of the Form for the page to my login button.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜