0

I have a GridView with a Textbox and when the user changes the text within that box, I want them to be able to hit the enter button and postback and update the changes they made within that textbox on the row in the GridView.

I can't figure out how to do this?

Thanks, Mark

Mark Smith
  • 41
  • 5
  • Bind a javascript onchange event to your textbox, track the value of input key and if it is enter button, fire a post back. Use following post to know how to postback using javascript - https://stackoverflow.com/questions/1305954/asp-net-postback-with-javascript – Piyush Khanna Jan 03 '19 at 17:23
  • @PiyushKhanna The solution on this answer you post, is not possible, because all controls are render by `GridView` dynamically. – Aristos Jan 03 '19 at 17:25
  • @Aristos - I just passed the idea, knowing a little detail. Of course, the controls are dynamic and need some jQuery to bind the keypress (i wrongly said onchage earlier). – Piyush Khanna Jan 03 '19 at 17:31
  • Great Thanks folks! – Mark Smith Jan 07 '19 at 18:42

1 Answers1

0

You need to use some JavaScript code to do that - its on the page part. Here is one that I use (jQuery)

$(document).ready(function(){
    // capture the editors
    var AllEditors = jQuery('#<%= gvGridViewID.ClientID %> :input[type=text]');

    AllEditors.keydown(function (e) {
        if (e.keyCode == 13) 
        {
            e.preventDefault();
            // the [value=Update] is the default value to Update control of GridView
            jQuery(this).parents("tr").find("input[value=Update]").click();
        }
    });
});

If you have it inside UpdatePanel, you need to initialize it each time the UpatePanel fires. If you have it inside a custom control, you need to add extra variables on the function names to avoid conflicts.

Aristos
  • 66,005
  • 16
  • 114
  • 150