views:

209

answers:

3

Hi,

I have an Asp.Net web page, having the common Asp.Net form. The outer "border" of the page (i.e. main menu, header, ...) is build using normal Asp.Net code using a master page. The content of that page uses jQuery to display dynamic forms and to send data to the server.

If I push the return key on that page, I jump to a (more or less) random page - which is not what the user expects. ;-)

There are some text areas and the user must be able to enter line breaks. Otherwise it would be fine to disable the return key completely. Any bullet proof way to do that?

I found some solutions on the web, which capture the keypress event and ignore \x13, but that does not really work. It works as long as the page has just loaded, but as soon as I have clicked on some elements, the return key behaves as usuall.

Any hint would be really appreciated!

Achim

A: 

You could achieve it by using Panel like below.

 <asp:Panel runat="server" DefaultButton="">
other html/asp tags
    </asp:Panel>

There should be other efficient ways.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.panel.defaultbutton.aspx

Saar
A: 

Here is some script I put together yesterday night but was not able to post it as my laptop ran out of battery :(

NOTE: I'm using jQuery for it. But you can easily re-write it to work with pure javascript.

<html>
<head>
<title>Enter Key</title>
<script src="jquery-1.2.6.js"></script>
</head>
<body>
<div id="prompt" style="color:#eeffee;width:50px;height:50px;"></div>
<textarea id="txt" cols="25" rows="10"></textarea>
<script type="text/javascript">
$(document).ready(function(){
    var ar=$("textarea").add($("input"));
    var elems=new Array();
    var key = 13;

    $(ar).each(function(){
     var i=$(this).attr("id");
     if(i && i!=="")
      elems.push(i);
    });
    elems.sort();

    $().keypress(function(e){
     var k=e.keycode || e.which;
     var id = e.target.id;
     if(k === key){
      if(id){
       var ar=$.grep(elems,function(a){ return a==id;});
       if(ar.length === 0){
        $("#prompt").html("Eating ");
          return false;
       }else{
        $("#prompt").html("Allowed");
       }    
      }else{
       $("#prompt").html("Eating ");
          return false;
      }   
     }
    });
});
</script>
</body>
</html>
TheVillageIdiot
A: 

Here is what I used to fix this problem.

<form runat="server" defaultbutton="DoNothing">
        <asp:Button ID="DoNothing" runat="server" Enabled="false" style="display: none;" />
Lone Coder