Feed on
Posts
Comments

Recently I found new bug in MS WebResource.axd javascript. The problem is that when using a browser like Firefox and a multiline textbox is put inside panel with DefaultButton set, pressing the enter button while editing text in the textbox submits the form instead of going to new line.
 
The fix that I’ve found is this JavaScript function that accepts the textbox client id as parameter:

function PreventSubmitOnKeyPress(sObjectID) {
    try {
        if (!document.all) {
            var oObject = $id(sObjectID);
           
            if (oObject.addEventListener) { 
                oObject.parentNode.addEventListener("keypress",
                    function(e) {
                        if (e && e.keyCode && e.keyCode==13) {
                            e.stopPropagation();
                        }
                    }, false); 
            } 
        }
    } catch (e) {}
}

 
To call the function you can use something like that:

<script type="text/javascript">
var sMyTextBoxID = ‘<%=txtMyTextbox.ClientID %>’;
PreventSubmitOnKeyPress(sMyTextBoxID);
</script> 

, ,
Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • dzone
  • Netscape
  • digg
  • YahooMyWeb
  • Technorati

3 Responses to “Prevent postback in FF invoked by textarea enter click (ASP.NET 2.0)”

  1. on 18 Aug 2007 at 3:25 pm OJ

    I have this problem and this multi lined textbox in a ajax updatepanel control. I already have an attached a javascript to this textbox “onkeyup”. What i haven’t understand here is how to call the javascript u written from my textbox. Can u please give me an idea. It’ll be a big help???

    OJ

  2. on 20 Aug 2007 at 8:39 am Ivan Uzunov

    Hi OJ, You do not have to attach the JS to the "onkeyup" event. Just add the second script in the bottom of your page and the function PreventSubmitOnKeyPress() will do the job:

    <script type="text/javascript">
    var sMyTextBoxID = ‘<%=txtMyTextbox.ClientID %>’;
    PreventSubmitOnKeyPress(sMyTextBoxID);
    </script>  

  3. on 31 Oct 2007 at 9:55 pm Brando

    Worked like a charm, once I replaced $id with document.getElementById().

Trackback URI | Comments RSS

Leave a Reply

This is a captcha-picture. It is used to prevent mass-access by robots. (see: www.captcha.net)

You must read and type the 5 chars within 0..9 and A..F, and submit the form.

  

Oh no, I cannot read this. Please, generate a