Link to home
Create AccountLog in
Avatar of skarlsen
skarlsenFlag for Norway

asked on

How to set focus on a textbox in GridView after a postback


I have a GridView inside an UpdatePanel with ItemTemplates which contains textboxes. They all use the same TextChange-event and when I hit TAB i want the cursor to move to the next textbox in the row after postback. That means the textbox it was supposed to move to if the postback was not triggered. Any idea on how to solve this?
Avatar of SandyAgo
Flag of Australia image

Avatar of skarlsen


Hi SandyAgo,

I have tried this sample but did not get it to work with GridView. What I did was that I assigned a tabindex to the textcontrols in the RowDatabound-event. The control that fired the Postback is returned with the correct TabIndex but the LINQ-query returned no controls, only null-value.
I have now attached my sourcecode.
Did you try maintainscrollpositiononpostback="true" in Page declarative ?

Also have a look at (also suggested links there for UpdatePanel):

Avatar of skarlsen
Flag of Norway image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
ScriptManager sm = ScriptManager.GetCurrent(this);
//To set focus and select text of a you have to use the following:
ScriptManager.RegisterStartupScript(this, this.GetType(), "selectAndFocus", "$get('" + yourTextBox.ClientID + "').focus();$get('" + yourTextBox.ClientID + "').select();", true);

No problem setting focus on a textbox when I know the ID of it, but this issue was to set focus on the next textbox after a postback, that means the textbox the user was about to TAB into when postback was triggered.
A new issue have occured. If I place a control which is not a TextBox on the page this Javascript fails. Therefore I have to check if it is a textbox that has focus when a postback is triggered. Unfortunately I do not know much JavaScript so any help regarding this is very appreciated.

The solution that you say works, is in the link provided by me. do you mind closing it yet..