Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 899027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:08:53+00:00 2026-05-15T15:08:53+00:00

Is it possible to put focus back on a gridview row after that a

  • 0

Is it possible to put focus back on a gridview row after that a selection of the row generates a postback?

I’m trying to add an onkeydown handler on the gridview rows in order to use the keyboard for navigation. My problem, I believe, is that after the first postback, the selected cell loses focus, and so the next key stroke is not caught by the cell.

I have the following code

The grid view

    <asp:GridView runat="server" ID="gdvPersons" AutoGenerateColumns="false" 
        onrowcreated="gdvPersons_RowCreated" onselectedindexchanged="gdvPersons_SelectedIndexChanged">
        <Columns>
            <asp:TemplateField HeaderText="Name">
                <ItemTemplate>
                    <%# ((GridviewFocus.Person) Container.DataItem).Name %>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Age">
                <ItemTemplate>
                    <%# ((GridviewFocus.Person) Container.DataItem).Age %>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

The Code behind

    protected void Page_Load(object sender, EventArgs e)
    {
        var persons = new List<Person> {new Person() {Name = "Fikre", Age = 24}, 
                                        new Person() {Name = "Mike", Age = 29},
                                        new Person() {Name = "Mark", Age = 35}};
        gdvPersons.DataSource = persons;
        gdvPersons.DataBind();
    }

    protected void gdvPersons_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
            e.Row.Attributes.Add("onkeydown", ClientScript.GetPostBackEventReference((Control)sender, "Select$" + e.Row.DataItemIndex));
    }

    protected void gdvPersons_SelectedIndexChanged(object sender, EventArgs e)
    {
        gdvPersons.SelectedRow.Focus();
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T15:08:54+00:00Added an answer on May 15, 2026 at 3:08 pm

    On your onkeydown script code copy the id of the cell to a hidden input field.

    <input type="text" id="gridviewcell_id" onkeydown="lastcell.value = this.id" />
    <input type="hidden" id="lastcell" runat="server" />
    

    the example above is plain html, and you would have to add the proper onkeydown code to your gridview.

    In your postback (for example onclick) event handler code you can retrieve the id from the hidden fields value property and register javascript to execute once the page is refreshed. If you have a button which is clicked which performs the postback you could do something like this:

    protected void MyButton_Click(object sender, EventArgs e)
    {
       string id = lastcell.Value;
       string script = "var ctrl = document.getElementById('" + lastcell.Value + "');";
       script += "ctrl.focus();";
       ClientScript.RegisterClientScriptBlock(this.GetType(), 
           "focusScript", script, true);
    
    }
    

    This should make your page execute the following script once it’s loaded, and the control should retrive focus:

    var ctrl = document.getElementById("yourid"); 
    ctrl.focus();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.