title: Put focus back on a gridview's selected row after postback
body: <p>Is it possible to put focus back on a gridview row after that a selection of the row generates a postback?</p>
  
  <p>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.</p>
  
  <p>I have the following code</p>
  
  <p>The grid view</p>
  
  <pre><code>    &lt;asp:GridView runat="server" ID="gdvPersons" AutoGenerateColumns="false" 
          onrowcreated="gdvPersons_RowCreated" onselectedindexchanged="gdvPersons_SelectedIndexChanged"&gt;
          &lt;Columns&gt;
              &lt;asp:TemplateField HeaderText="Name"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;%# ((GridviewFocus.Person) Container.DataItem).Name %&gt;
                  &lt;/ItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
              &lt;asp:TemplateField HeaderText="Age"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;%# ((GridviewFocus.Person) Container.DataItem).Age %&gt;
                  &lt;/ItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
          &lt;/Columns&gt;
      &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>The Code behind</p>
  
  <pre><code>    protected void Page_Load(object sender, EventArgs e)
      {
          var persons = new List&lt;Person&gt; {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();
      }
  </code></pre>