Solved

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

Posted on 2010-09-09
11
4,122 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

615 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