views:

2349

answers:

5

I have a GridView defined like this:

<asp:GridView ID="myGridView" ruant="server">
    <asp:BoundField DataField="myField" />
    <asp:CommandField ShowDeleteButton="true" ShowEditButton="true" />
</asp:GridView>

After I put a row into edit mode with the Edit button, how do I capture the Enter key and trigger the resulting Update on the row? Right now if I hit enter, the page reloads, what was entered into the TextBox is lost, and the row stays in edit mode. I know how to disable the enter key entirely on the form (the current workaround), but I'd like to have it fire the Update command.

+1  A: 

Well, using the knowledge from the question you linked, it's simple:

string js = "if (event.keyCode == 13) this.form.submit();"
myGridView.Attributes.Add("onkeydown", js);

As we found out in the comments, this introduces a small problem. The GridView_RowUpdating server event does not fire anymore, but the question author relies on it.

In short - the server event model relies on the form field __EVENTTARGET to be set. This form field is not sent when just calling the form.submit(). A solution would be to "click" the relevant button with JavaScript.

string js = "if ((event.which && event.which == 13) || " 
            + "(event.keyCode && event.keyCode == 13)) "
            + "{document.myForm.Update.click();return false;} "
            + "else return true;";
myGridView.Attributes.Add("onkeydown", js);

See "Using the enter key to submit a form" on AllAsp.net, which covers the issue in more detail.

Tomalak
No, that just mimics the behavior as if there were no "onkeydown" event.
gfrizzle
Can you explain? For me this submits the form.
Tomalak
(Please check that the "submit" button is not named "submit", as this overrides the form.submit() function.)
Tomalak
No, in this case the button is named Update. Submitting the form does not trigger the GridView_RowUpdating event, where the underlying update happens.
gfrizzle
But the form is submitted alright?
Tomalak
Yes, but by not triggering the event to write the new value, it's no different than refreshing the page.
gfrizzle
Thinking about it, I have a suspicion. The event cannot fire when the event source is unclear to the server (e.g. "which button was klicked?"). But in this case no button is clicked. I changed my code sample a bit. Please confirm if it works!
Tomalak
I've done something similar before, and it may be the only way around this. Thanks for hashing this out with me.
gfrizzle
A: 

Where should be the code placed?

A: 

Set "UseSubmitBehavior" property of buttons on the page to "False" (default is True) solves the submit issue in several comments.

A: 

The code suggested above is not quite right. This code

document.myForm.Update.click()

suggests that the button name is Update which is not since it is IDless and nameless since it is <asp:CommandField ShowEditButton="True" ...>

If I assign ID to it programatically within the gridview onrowdatabound event:

Dim tmpButton1 As Button = CType(e.Row.Cells(4).Controls(2), Button)
If Not tmpButton1 Is Nothing Then tmpButton1.ID = "btnMyUpdate"

I still can't access it through javascript cause it generates different name for each row:

GridView1_ctl02_btnMyUpdate or GridView1$ctl02$ctl00 or GridView1$ctl03$ctl00 etc.

Please help

Dabac
You should post follow-up questions as a separate thread, notas an answer. After all, it doesn't really answer *this*question. Also more people would see it and try to answer if you post it asyour own question.
sth
A: 

How can this be done in C#?

Dean
You should post follow-up questions as a separate thread, notas an answer. After all, it doesn't really answer *this*question. Also more people would see it and try to answer if you post it asyour own question.
sth