Setting focus on filter field

7 posts, 0 answers
  1. Josh
    Josh avatar
    1 posts
    Member since:
    Jun 2011

    Posted 26 Jun 2011 Link to this post

    If a particular filter operation does not yield any results, we would like to be able to set focus on the last filter field that the user put data into. For example, suppose I have a "Status" column with options of "All","Active","Inactive". If a user picks "Inactive" and there are no records matching that criteria I would like to set focus on the filter control for the "Status" column.

    I may just be missing something obvious, but I'm not having luck with this so far.

    Thank you!
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 27 Jun 2011 Link to this post

    Hello Josh,

    In order to achieve this, store the ColumnUniqueName in a session variable in ItemCommand event. Access the  session variable in PreRender and then set the focus of filter textbox. Hope this helps.

    C#:
    protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
         if (e.CommandName == RadGrid.FilterCommandName)
         {   
           Pair filterPair = (Pair)e.CommandArgument;
           Session["filter"] = filterPair.Second.ToString();//storing ColumnUniqueName in session variable
         }
    }
    protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
            int count = RadGrid1.Items.Count;
            if (count==0)
            {
                GridFilteringItem filteringItem = (GridFilteringItem)RadGrid1.MasterTableView.GetItems(GridItemType.FilteringItem)[0];         
                string ColumnUniqueName = Session["filter"].ToString();
                TextBox box = filteringItem[ColumnUniqueName ].Controls[0] as TextBox;
               box.Focus();
            }
    }

    Thanks,
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Siward Land
    Siward Land avatar
    10 posts
    Member since:
    Mar 2010

    Posted 30 Jun 2011 Link to this post

    I would like to do the same thing.  However, is there a way to do it without creating a session?
  5. Siward Land
    Siward Land avatar
    10 posts
    Member since:
    Mar 2010

    Posted 30 Jun 2011 Link to this post

    I just changed to session part to use viewstate.  it works.  Thanks.
  6. Mike
    Mike avatar
    11 posts
    Member since:
    Jul 2010

    Posted 16 Sep 2011 Link to this post

    You can also store it on HttpContext.Current.Items which lasts for the duration of the current page request:

    in Grid_ItemCommand:
    HttpContext.Current.Items["FilterColumnName"] = filterPair.Second.ToString();


    in Grid_PreRender:
    string columnUniqueName = HttpContext.Current.Items["FilterColumnName"].ToString();
  7. tamir
    tamir avatar
    5 posts
    Member since:
    May 2009

    Posted 05 Aug 2015 Link to this post

    i had a little modification too so

    it filters right away

    put the focus on the last filter element 

    put the cursor at the end of the fitered text

     protected void RadGrid1_PreRender(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GridFilteringItem filteringItem = (GridFilteringItem)RadGrid1.MasterTableView.GetItems(GridItemType.FilteringItem)[0];

                string columnUniqueName = HttpContext.Current.Items["FilterColumnName"].ToString();
                if (!string.IsNullOrEmpty(columnUniqueName))
                { 
                    TextBox box = filteringItem[columnUniqueName].Controls[0] as TextBox;
                    box.Attributes.Add("onFocus", "this.value=this.value"); // setting the cursor to the end of the text !!!
                    box.Focus();// focus the filter element
                }
            }
        }​

     

    protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == RadGrid.FilterCommandName)
            {
                Pair filterPair = (Pair)e.CommandArgument;
                HttpContext.Current.Items["FilterColumnName"] = filterPair.Second.ToString();
            }
        }

     

    <telerik:GridBoundColumn DataField="token" FilterControlAltText="Filter token column" HeaderText="token" SortExpression="token" UniqueName="token"
                                                         CurrentFilterFunction="Contains" AutoPostBackOnFilter="false" FilterDelay="1"  ShowFilterIcon="false">​​

  8. Eyup
    Admin
    Eyup avatar
    3014 posts

    Posted 07 Aug 2015 Link to this post

    Hi Tamir,

    I am attaching a sample RadGrid web site to demonstrate an alternative solution for this case.

    Regards,
    Eyup
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017