Save records by hitting a enter key

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 all button to be able to save the records that they just entered.

I was using below code to be able to achive the goal.
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);");
    }
}


the functionally of this coce 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.

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]...

wha I am doing wrong here??
jung1975Asked:
Who is Participating?
 
SammyConnect With a Mentor Commented:
try this in your page_load
Textbox2.Attributes.Add("onkeydown", "doFocus(window.event.keyCode);");
if this doesnt work, use findcontrol and attach the client script event

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.