Solved

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

Posted on 2010-09-09
11
3,802 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
 
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now