开发者

asp.net imagebutton firing on Enter key

I researched this problem here on SO and tried the apparent solution which did not work, so here it is:

I have a very complex form with among other controls, three autocompleting textboxes.

I also have a client who cannot seem to stop entering a value in the textboxes and hitting the Enter key to select the desired value from the autocomplete list.

When they hit Enter, the first imagebutton in the form fires, doing something completely different. So to them,开发者_运维知识库 the form is broken.

What I need to do is to prevent the Enter key from firing these imagebuttons (there are 10 of them in the form).

I have tried the following code in both Page_Load and Page_LoadComplete, neither of which work:

imgbtn1.Attributes.Add("onkeydown", "return (event.keyCode!=13);")

Any advice that saves me a few hairs is appreciated.


One good solution can be found here:

Disable Button click, ImageButton click and/or form submit on Enter Keypress

Adding Markup from Link

<form id="form1" runat="server"> 
<input type="submit" style="position:absolute;left:-100px;top:-100px;width:0px;height:0px;" onclick="javascript:return false;" /> 
<!-- other form controls below this line -->  
</form>


Did you check these two references

http://www.bloggingdeveloper.com/post/Disable-Form-Submit-on-Enter-Key-Press.aspx

http://www.webcheatsheet.com/javascript/disable_enter_key.php

They are essentially doing the same thing you are trying, just that they are hooking it up to a different event. Also make sure that your Javascript is foolproof i.e. if javascript has some errors, then your end result may not be as expected.


The second link Subhash Dike posted (Disable Enter Key) worked for me. I have two ImageButtons and they both don't fire a postback when using this function (bit modified from the original) which is great.

            document.onkeypress = function (evt) {
            return ((evt) ? evt : ((event) ? event : null)).keyCode != 13;
        };


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.Page.Form.DefaultButton = BtSearch.UniqueID
        If Not IsPostBack Then
           ' ...............
        End If
End Sub


I had a similar problem. Listview1.ImageButton was responding to ENTER. We really want a SAVE to happen (or nothing, but the bad behaviour was reloading the page and that made them grumpy)

  1. Set up a handler for the window to catch events. I do this in docReady()

    function docReady() {
    
     $(document).keydown(mapKeyCode);
    
    };
    
  2. In that handler, find the keycode you want to capture ( enter is 13)

https://css-tricks.com/snippets/javascript/javascript-keycodes/

     function mapKeyCode(event) {
    {


        if (event.keyCode == 13) {
            //event.stopPropagation();
            //event.preventDefault();
            __doPostBack('ctl00$cp1$InventoryPieces$btnSubmit', '');

        }
    }

event.StopPropogation will cause nothing to happen.The keystroke is simply eaten. That may be what you want. You are still free to call an ajax method below, that's not part of the event.

event.PreventDefault is supposed to stop the event from doing what it normally does. I had troubles seeing a difference w/ this line commented out or not. There is in depth discussion on preventDefault and ENTER here on SO.

This 3rd line is what the people who pay me want to have happen when they hit the ENTER key even though they probably should be hitting tab. I tried (#).trigger() and didn't have a lot of luck. Inspecting the element, I saw that it was calling __postback, so I pasted that in. I'm reasonably certain ().Trigger would work if i figured out what i was doing wrong, I just took another route.

This is hackish to me, but it accomplishes the objective.

Hope it helps.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜