Solved

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

Posted on 2010-09-09
11
3,970 Views
Last Modified: 2012-06-27
Hi,

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?
0
Comment
Question by:skarlsen
11 Comments
 
LVL 2

Expert Comment

by:SandyAgo
ID: 33636810
0
 
LVL 1

Author Comment

by:skarlsen
ID: 33636940
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.
0
 
LVL 1

Author Comment

by:skarlsen
ID: 33637496
I have now attached my sourcecode.
GridViewSums.zip
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 9

Expert Comment

by:Valliappan AN
ID: 33638180
Did you try maintainscrollpositiononpostback="true" in Page declarative ?

0
 
LVL 9

Expert Comment

by:Valliappan AN
ID: 33638207
Also have a look at (also suggested links there for UpdatePanel):

http://forums.asp.net/p/1407512/3084466.aspx

Thanks
0
 
LVL 1

Accepted Solution

by:
skarlsen earned 0 total points
ID: 33638595
Looks like this solves my issue:

1) attach Onfocus javascript event to all your embedded textboxes -:
 
  protected void grd_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ((TextBox)e.Row.FindControl("txt1")).Attributes.Add("onfocus", "onTextFocus();");
            ((TextBox)e.Row.FindControl("txt2")).Attributes.Add("onfocus", "onTextFocus();");
            ((TextBox)e.Row.FindControl("txt3")).Attributes.Add("onfocus", "onTextFocus();");
            ((TextBox)e.Row.FindControl("txt4")).Attributes.Add("onfocus", "onTextFocus();");
        }
    }

2) In the script block of aspx file add the following code -:

var g_CurrentTextBox;
Sys.Application.add_load(applicationLoadHandler);
function applicationLoadHandler() {
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandler);
}

function endRequestHandler() {
    $get(g_CurrentTextBox).focus();
    $get(g_CurrentTextBox).select();
}

function onTextFocus()
{
   g_CurrentTextBox =  event.srcElement.id;
}
 
0
 
LVL 5

Expert Comment

by:vivekpv10
ID: 33643570
ScriptManager sm = ScriptManager.GetCurrent(this);
sm.SetFocus(yourTexttBox);
//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);
0
 
LVL 1

Author Comment

by:skarlsen
ID: 33643892
vivekpv10:

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.
0
 
LVL 1

Author Comment

by:skarlsen
ID: 33644186
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.
0
 
LVL 1

Author Comment

by:skarlsen
ID: 33645085
Solved...
0
 
LVL 9

Expert Comment

by:Valliappan AN
ID: 33652459
skarlsen,

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

thanks
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question