I have a gridview that uses an imagebutton as an update button. When the user edits a row I would like the user to be able to update the row by pressing Enter. The problem I'm having is that though the RowCommand event is fired, the CommandName is still "Edit" instead of "Update so my update code never gets executed. What I've done is hook up some javascript to the textbox in the RowDataBound handler:
Protected Sub uxShoppingCart_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim gvr As GridViewRow = DirectCast(e.Row, GridViewRow)
If (e.Row.RowState And DataControlRowState.Edit) = DataControlRowState.Edit Then
Dim edit_Qty As TextBox = DirectCast(gvr.FindControl("edit_Qty"), TextBox)
Dim uxUpdateButton As ImageButton = DirectCast(gvr.FindControl("uxUpdateButton"), ImageButton)
uxQty.Attributes.Add("onkeypress", "return clickButton(event,'" & uxUpdateButton.ClientID & "')")
edit_Qty.Focus()
End If
End If
End Sub
The javascript function is as follows:
function clickButton(e, buttonid) {
var bt = document.getElementById(buttonid);
if (typeof bt == 'object') {
if (navigator.appName.indexOf("Netscape") > (-1)) {
if (e.keyCode == 13) {
bt.click();
return false;
}
}
if (navigator.appName.indexOf("Microsoft Internet Explorer") > (-1)) {
if (event.keyCode == 13) {
bt.click();
return false;
}
}
}
}
And here's the button declaration
<EditItemTemplate>
<span style="display: inline; white-space: nowrap;">
<asp:ImageButton ID="uxUpdateButton" runat="server" CausesValidation="True" CommandName="Update" ImageUrl="~/images/icons/accept.png" AlternateText="Update" ToolTip="Update"></asp:ImageButton>
<asp:ImageButton ID="uxCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" ImageUrl="~/images/icons/cancel.png" AlternateText="Cancel" ToolTip="Cancel"></asp:ImageButton>
</span>
</EditItemTemplate>
If I click the update button using the mouse the CommandName "Update" gets passed as expected, just not when I "click" it via javascript. Any help would be appreciated.