Solved

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

Posted on 2010-09-09
11
4,014 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

749 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