I want ASP.NET GridView not to respond to enter button

I have an ASP.NET page with a gridview control on it with a CommandButton column with active delete and select commands.

Pressing the enter key triggers the first command button in gridview, which deletes the row. I do not want this to happen. Can I change the gridview control so that it no longer responds to pressing the enter key?

There is a text box and a button on the screen. They do not need to respond to a hit, but you should be able to fill out a text box. We are currently opening a confirmation dialog to prevent accidental deletion, but we need something better than this.

This is the markup for gridview, as you can see it inside the asp.net update panel (I forgot to mention this, sorry): (I missed most of the columns and formatting)

<asp:UpdatePanel ID="upContent" runat="server" UpdateMode="Conditional"> <Triggers> <asp:AsyncPostBackTrigger ControlID="btnFilter" /> <asp:AsyncPostBackTrigger ControlID="btnEdit" EventName="Click" /> </Triggers> <ContentTemplate> <div id="CodeGrid" class="Grid"> <asp:GridView ID="dgCode" runat="server"> <Columns> <asp:CommandField SelectImageUrl="~/Images/Select.GIF" ShowSelectButton="True" ButtonType="Image" CancelText="" EditText="" InsertText="" NewText="" UpdateText="" DeleteImageUrl="~/Images/Delete.GIF" ShowDeleteButton="True" /> <asp:BoundField DataField="Id" HeaderText="ID" Visible="False" /> </Columns> </asp:GridView> </div> </ContentTemplate> </asp:UpdatePanel> 
+4
source share
6 answers

This solution blocks the input key throughout the page.

Disable input key

+1
source

From time to time I get stupid problems like this one too ... but usually I just implement a quick hack and move on :)

 myGridView.Attributes.Add("onkeydown", "if(event.keyCode==13)return false;"); 

Something like this should work.

+4
source

In the PreRender event you can switch

 Private Sub gvSerials_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvSerials.PreRender If gvSerials.EditIndex < 0 'READ ONLY MODE 'Enables the form submit during Read mode on my 'search' submit button Me.bnSearch.UseSubmitBehavior = True Else 'EDIT MODE 'disables the form submit during edit mode, this allows the APPLY/Update button to be activated after Enter Key is pressed (which really is creating a form submit) Me.bnSearch.UseSubmitBehavior = False End If 
+1
source

In Page_Load, set the focus to the text box.

0
source

Here is a good jQuery way. This function will automatically add a keydown event handler to all text fields on the page. Using various selectors, you can control it to more or less granular levels:

 //jQuery document ready function – fires when document structure loads $(document).ready(function() { //Find all input controls of type text and bind the given //function to them $(":text").keydown(function(e) { if (e.keyCode == 13) { return false; } }); }); 

This will cause all text fields to ignore the input key and have the advantage of automatically applying to any new HTML or ASP.Net controls that you can add to the page (or that can be generated by ASP.Net).

0
source

For everyone who has the same problem, when this code does not interfere with the event from the launch, this worked for me:

 if (window.event.keyCode == 13) { event.returnValue=false; event.cancel = true; } 
0
source

Source: https://habr.com/ru/post/1277309/


All Articles