Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2010-09-09
11
Medium Priority
?
4,279 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

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!

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

972 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