ASP.NET - Set focus to grid view after search - Asked By Vimal Sharma on 13-Feb-12 03:04 AM

I have three textbox through which user can search and when he clicked on search button then its postback and give the resultant grid view with checkboxes with each row . But the tab and focus is set to the initial page because of page . But i want it to be on grid view row .
Web Star replied to Vimal Sharma on 13-Feb-12 03:14 AM
you simply set the currentcell property of gridveiw to set focus on that grid after postback the page.

dataGridView1.CurrentCell = dataGridView1[1, idx]; 
kalpana aparnathi replied to Vimal Sharma on 13-Feb-12 03:15 AM
Try this with this example
Put
this in your page_load
 
Code:
 
Utils.FocusControlOnPageLoad(this.GridView1.Client  ID, this.Page)
 
 
 
 
add this public class
Code:
 
public class Utils
{
  public static void FocusControlOnPageLoad(string ClientID, System.Web.UI.Page page)
  {
       
     
     
page.RegisterClientScriptBlock("CtrlFocus",
"<script> " + Strings.Chr(13) + "" + Strings.Chr(10) + "" + Strings.Chr(13) + "" + Strings.Chr(10) +
" function ScrollView()" + Strings.Chr(13) + "" + Strings.Chr(10) + "" + Strings.Chr(13) + "" + Strings.Chr(10) + " {" + Strings.Chr(13) + "" + Strings.Chr(10) +
" var el = document.getElementById('"
+ ClientID + "')" + Strings.Chr(13) + "" + Strings.Chr(10) +
" if (el != null)"
+ Strings.Chr(13) + "" + Strings.Chr(10) + " { " + Strings.Chr(13) + "" + Strings.Chr(10) +
" el.scrollIntoView();"
+ Strings.Chr(13) + "" + Strings.Chr(10) +
" el.focus();" + Strings.Chr(13) + "" + Strings.Chr(10) + " }" + Strings.Chr(13) + "" + Strings.Chr(10) + " }" + Strings.Chr(13) + "" + Strings.Chr(10) + "" + Strings.Chr(13) + "" + Strings.Chr(10) +
" window.onload = ScrollView;"
+ Strings.Chr(13) + "" + Strings.Chr(10) + "" + Strings.Chr(13) + "" + Strings.Chr(10) +
" </script>");
     
  }
}
D Company replied to Vimal Sharma on 13-Feb-12 03:25 AM
Hello Friend,

As u have mentioned that u bind the search data to your gridview control, at the same time once data gets populated u can set the focus to first row as well, like this

protected void YourGridName_RowDataBound(object sender, GridViewRowEventArgs e)
    {
      if (e.Row.Cells[1].Controls.Count > 0)
        e.Row.Cells[1].Controls[0].Focus();
    }

Hope its clear!!
Regards
D
Somesh Yadav replied to Vimal Sharma on 13-Feb-12 03:55 AM
Put this in your page_load

Code:

Utils.FocusControlOnPageLoad(this.GridView1.Client  ID, this.Page)




add this public class
Code:

public class Utils
{
    public static void FocusControlOnPageLoad(string ClientID, System.Web.UI.Page page)
    {
      
      
      
      page.RegisterClientScriptBlock("CtrlFocus", "<script> " + Strings.Chr(13) + "" + Strings.Chr(10) + "" + Strings.Chr(13) + "" + Strings.Chr(10) + " function ScrollView()" + Strings.Chr(13) + "" + Strings.Chr(10) + "" + Strings.Chr(13) + "" + Strings.Chr(10) + " {" + Strings.Chr(13) + "" + Strings.Chr(10) + " var el = document.getElementById('" + ClientID + "')" + Strings.Chr(13) + "" + Strings.Chr(10) + " if (el != null)" + Strings.Chr(13) + "" + Strings.Chr(10) + " { " + Strings.Chr(13) + "" + Strings.Chr(10) + " el.scrollIntoView();" + Strings.Chr(13) + "" + Strings.Chr(10) + " el.focus();" + Strings.Chr(13) + "" + Strings.Chr(10) + " }" + Strings.Chr(13) + "" + Strings.Chr(10) + " }" + Strings.Chr(13) + "" + Strings.Chr(10) + "" + Strings.Chr(13) + "" + Strings.Chr(10) + " window.onload = ScrollView;" + Strings.Chr(13) + "" + Strings.Chr(10) + "" + Strings.Chr(13) + "" + Strings.Chr(10) + " </script>");
      
    }
}


smr replied to Vimal Sharma on 14-Feb-12 12:03 AM
hi

try this

var ctrl = document.getElementById("yourid");
ctrl.focus();


refer
http://stackoverflow.com/questions/3181540/put-focus-back-on-a-gridviews-selected-row-after-postback
http://forums.aspfree.com/net-development-11/set-focus-on-selected-gridview-row-after-post-back-243894.html