2

I'm writing an in-house intranet application in ASP.NET and VB.NET. My 'customers' are beginner to medium-level users. All of our browsers are IE8 and above, standard.

The application works great, except for one thing. The backspace key. When a user types an invalid number into a textbox, a RegularExpressionValidator and ValidatorCalloutExtender fire off and notify the user. Perfect. Except that, when the user closes the popup warning and notices the cursor is still flashing in the textbox, he/she feels it's time to hit the backspace key and delete that pesky field value.

Unfortunately, the browser interprets this action as a desire to go 'back' into the page history. My boss is screaming. His bosses are screaming. I have a headache.

So, how can I turn off this behavior? I still need the backspace to eliminate characters in the textbox, but nothing else. Company policy here: Backspace is to delete characters from the screen. Nothing more, nothing less.

Any ideas?

Thanks,

Jason

  • would capturing it using javascript be a viable option? – masfenix Jun 29 '11 at 20:24
  • Could you please clarify the 'flashing' thing? the cursor focus is obtained by faulted input field? If yes, than it's strange that browser captures it instead of the control. Try stop the bubbling up event on the body level. – Arthur P Jun 29 '11 at 20:28
  • @masfenix: I have no idea how to do this. 'document' object??? –  Jun 29 '11 at 20:28
  • @Arthur: 'Flashing' is what cursors do, it's the blinking hash mark we see when typing -- looks like this: | but flashing on and off. –  Jun 29 '11 at 20:29
  • Understood. Please see my comment below: http://stackoverflow.com/questions/6526784/asp-net-backspace-key-behavior/6526885#6526885 – Arthur P Jun 29 '11 at 20:33

5 Answers5

1

You could try to disable caching, but that might have performance consequences. I think there is a javascript function that will fix it however:

window.history.forward();

Pair this with in a custom function like so

<script type=javascript>

function disableBack() { window.history.forward(); } setTimeout("disableBack()", 0);

</script>

Then call it in your <body onLoad="disableBack()">

I think this should work, I used it a long time ago however. Let me know if it works!

ginman
  • 1,315
  • 10
  • 20
  • Ah nevermind, I think the solution might be trickier than this - check out http://www.bloggingdeveloper.com/post/JavaScript-History-Object-History-Objects-Methods-and-Properties.aspx if you are still interested. – ginman Jun 29 '11 at 20:49
  • So far, disableBack works good enough for me. It'll probably come back to bite me in the behind, but with my workload I'll take the chance. Thanks! –  Jun 29 '11 at 20:53
0

Perhaps not using a popup in this situation would be a good approach.

The problem is that no acceptable control has focus and without forcing the focus to be said control, the browser will catch it instead.

ChrisBint
  • 12,773
  • 6
  • 40
  • 62
  • As the popup is in the design specs, not using it would probably get me fired. Plus, eliminating the popup won't fix the backspace key behavior. –  Jun 29 '11 at 20:28
  • @jason. If the control does not lose focus, the backspace will affect that and not the browser window. To be honest, in this day and age, I find popup of that natures being replaced by something a little bit friendlier, such as
    with the associated message
    – ChrisBint Jun 29 '11 at 20:30
  • I get what you're saying, but removing the popup is out of the question. The boss of my boss wrote the specs and I can't get away from that. However, I do see what you mean about focus.. –  Jun 29 '11 at 20:32
  • @jason. Does it have to be the standard 'Alert'. If not, it may be possible to incorporate a popup that uses a hidden div, similiar to a 'lightbox' to display it's message. You might then be able to attach a 'close' link to this which sets the focus before calling Window.Close. It is a popup, just not as we know it! – ChrisBint Jun 29 '11 at 20:36
0

When a user closes the popup, can you use that onClick event to set the form's focus back to the TextBox? I would think this would allow the user to resume editing the TextBox value.

Jemes
  • 2,822
  • 21
  • 22
  • Tried, but got the classic "Type AjaxControlToolkit.ValidatorCalloutExtender does not have a public property named 'onclick'. Thanks, though! –  Jun 29 '11 at 20:37
0

I see 2 possible solutions :

  1. Trap the keypress in javascript in your page and prevent it from bubbling up.
  2. Remove the popup warning, and use the "display=dynamic" with a summurize validation.
David
  • 429
  • 1
  • 4
  • 10
0

If the cursor is flashing in a text box, then the backspace should be handled by the input box as well and not supposed to get propagated to a document object or window object. If it does, you need to debug or use this technique to stop propagation on document level

Arthur P
  • 1,050
  • 9
  • 16