Save the recordinside of a textbox by hitting "Enter key" in gridview

just hitting the "Enter" key.
I have textboxes( item template)  inside of a gridview  and a form view

((TextBox)row.Cells[11].FindControl("TextBox2")).Text;

The use can enter a number inside of the textbox and click save button to be able to save the record that they just entered.

Is there anyway the user can just hit the "Enter" key to be able to save the record that they just entered? the user want to be able to save the record by just hitting the "enter" key.. I have no idea how I am going to so this..

I am using asp.net 2.0 c#..
jung1975Asked:
Who is Participating?
 
Elvio Lujan.Net Senior DeveloperCommented:
the functionally of the comment posted is: when the user press the enter key in the textbox then set the focus to the save button... it's like the user do click on the button
0
 
Elvio Lujan.Net Senior DeveloperCommented:
<script>
function doFocus(key)
{
    if(key == 13) document.getElementById('theIdOfTheButton').focus();
}
</script>

and in the rowdatabound

((TextBox)e.Row.Cells[11].FindControl("TextBox2")).Attributes.Add("onkeydown", "doFocus(window.event.keyCode);");
0
 
jung1975Author Commented:
Thanks, but I don;t understand your code:   if(key == 13)??

and Is this code "((TextBox)e.Row.Cells[11].FindControl("TextBox2")).Attributes.Add("onkeydown", "doFocus(window.event.keyCode);");"  will let the user save the record that they just entered inside of textbox in gridview?

0
 
jung1975Author Commented:
Thanks, but I am getting an error says: ArgumentOutofRangeException was unhandled by user code. Specified argument ws out of the range of valid values" I made sure that Textbox2 is Cells[11]...

I am not sure what I am doing wrong?


Here is the code

 function doFocus(key)
{
    if(key == 13) document.getElementById('Button2').focus();
}
 
 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        ((TextBox)e.Row.Cells[11].FindControl("TextBox2")).Attributes.Add("onkeydown", "doFocus(window.event.keyCode);");
    }
}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.