This is a migrated thread and some comments may be shown as answers.

Setting focus on filter field

6 Answers 104 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Josh
Top achievements
Rank 1
Josh asked on 26 Jun 2011, 05:10 AM
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!

6 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 27 Jun 2011, 09:35 AM
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.
0
Siward Land
Top achievements
Rank 1
answered on 30 Jun 2011, 07:31 PM
I would like to do the same thing.  However, is there a way to do it without creating a session?
0
Siward Land
Top achievements
Rank 1
answered on 30 Jun 2011, 07:46 PM
I just changed to session part to use viewstate.  it works.  Thanks.
0
Mike
Top achievements
Rank 1
answered on 16 Sep 2011, 08:19 PM
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();
0
tamir
Top achievements
Rank 1
answered on 05 Aug 2015, 09:03 AM

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">​​

0
Eyup
Telerik team
answered on 07 Aug 2015, 02:48 PM
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
Tags
Grid
Asked by
Josh
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Siward Land
Top achievements
Rank 1
Mike
Top achievements
Rank 1
tamir
Top achievements
Rank 1
Eyup
Telerik team
Share this question
or