1

I have a textbox in web form(asp.net). when user already keyin data, then they will press Enter key, so that the data will be update to database.

Is there any way possible to perform this?

6
  • Can you elaborate on " when user already keyin data"? In general, you cannot handle the clientside keydown-event on serverside. But you can handle the TextChanged event which is raised when the text was changed and the TextBox lost focus(you need to set AutoPostBack to true if you want it to be reaised immediately). Commented Dec 10, 2012 at 15:10
  • Isn't pressing Enter once a form has been filled out the same as clicking/actioning a "Submit" button for the form in question? Commented Dec 10, 2012 at 15:16
  • what I mean is that When user already typed information, they want to update to textbox. they want to have a single click (Enter Key) to do update operation. Commented Dec 10, 2012 at 15:20
  • lukeHennerley, you are right. That is what user want. How could I do so ? Commented Dec 10, 2012 at 15:21
  • Do you need solution with Jquery or only javascript? Commented Dec 10, 2012 at 15:40

2 Answers 2

1
function InitTextBox() {
//var _txt = $('#<%= txt.ClientID%>'); or
var _txt = $('input:[id*="txtId"]');
if (_txt.length < 1) return;
_txt.get(0).autocomplete = 'off';
_txt.on('keydown', function (evt) {
    var _self = $(this);
    event = evt ? evt : window.event;
    if (event.keyCode == 13) {
        if ($.browser.msie)
        { event.cancelBubble = true; event.returnValue = false; }
        else { event.preventDefault(); }
        if (_self.val().length > 0 && _self.val().match(/^\s*$/) == null)
            __doPostBack(_self.attr('name'), '');
    }
});
}
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(InitTextBox);

And set autopostback for textbox to false, please

Sign up to request clarification or add additional context in comments.

Comments

1

Dino Esposito had wrote about this a while back. You wouldn't necessarily need a custom control, but can use his JavaScript.

If you have a control wrapped around a Panel, and you have the DefaultButtonID property set, the panel will trigger a postback on enter too by clicking the desired button specified by the ID. That's another way.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.