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

How to persist DropDownList selected index and value on postback from RadGrid FilterTemplate

10 Answers 2280 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Carl
Top achievements
Rank 1
Carl asked on 17 Apr 2009, 06:15 AM
I am using a <FilterTemplate> in a <telerik:GridTemplateColumn> for which I have this DropDownList:

<

 

asp:DropDownList ID="ddlEntityTypeFilter" runat="server" AutoPostBack="true"

 

 

 

OnSelectedIndexChanged="ddlEntityTypeFilter_SelectedIndexChanged">

 

 

 

I have succeeded in getting the postback to work correctly so that the RadGrid is updated after a telRadGrid.Rebind().
And everything seems to work EXCEPT the DropDownList does NOT persist the SelectedIndex. I can retain the SelectedIndex in a session variable, but I have not been able to reset the SelectedIndex so that it shows the correct index that was selected for the postback.

Which DropDownList event, and/or RadGrid event, and/or Page event should I be using? Any code to show how to do this would be most appreciated. Thanks.

10 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 17 Apr 2009, 12:44 PM
Hi Carl,

Try the following code snippet for resetting the DropDownList to correct one (which is placed in FilterTemplate) after rebinding the grid and see whether it is working fine for you.

CS:
 
protected void RadGrid1_PreRender(object sender, EventArgs e)  
{  
    foreach (GridFilteringItem filterItem in RadGrid1.MasterTableView.GetItems(GridItemType.FilteringItem))  
    {  
        DropDownList dropdown = (DropDownList) filterItem.FindControl("ddlEntityTypeFilter");  
        if (Session["slectedValue"]!=null)  
        dropdown.SelectedIndex = Convert.ToInt32(Session["slectedValue"]);  
    }  
}  
protected void ddlEntityTypeFilter_selectionChanged(object sender, EventArgs e)  
{  
    // Filtering logic  
    DropDownList dropdown = (DropDownList) sender;  
    Session["slectedValue"] = dropdown.SelectedIndex; // Saving the selected index in session variable  

Thanks,
Princy.
0
Carl
Top achievements
Rank 1
answered on 17 Apr 2009, 04:00 PM
Thanks for your help. The PreRender event worked great.
0
IT-Fjárvakur
Top achievements
Rank 1
answered on 31 Jul 2015, 12:20 PM

So I was wondering about the dropdownlist since it has been giving me a lot of problems. I used similar method (using dropdownlists inside filtertemplate inside a radgrid) and when I rebind the grid ​as I change values in the droplists, a conflict occurs between the two droplists.

I have two dropdownlists that have custom made filter search. As I set the first dropdownlist to Joe (Value 3) the grid filters Joe and everything is fine. When I click on my next dropdownlist and choose England, the filter should filter all Joes who are in england. But since the first dropdownmenu has reset, the value of it will only display "empty" or 0, so my radgrid only display everyone who is in england but not the Joes.

Is this understandable? Here is some code.

The only error I get is
"Unable to cast object of type 'Telerik.Web.UI.RadDropDownList' to type 'System.Web.UI.WebControls.DropDownList'."

If anynone has some tips for this, please explain. You can provide external links but I have google'd A LOT about this and I don't seem to find the answer (reason why I'm waking up a 6 year old post :) )

 

<telerik:RadGrid ID="RadGrid1"
        runat="server"
        AllowPaging="True"
        AllowSorting="True"
        DataSourceID="BasWareData"
        GroupPanelPosition="Top"
        ShowGroupPanel="True"
        EnableLinqExpressions="False"
        AllowFilteringByColumn="True"
        OnPreRender="RadGrid1_PreRender">
        <ClientSettings AllowDragToGroup="True" AllowColumnsReorder="True">
            <Selecting CellSelectionMode="SingleCell" />
            <Scrolling AllowScroll="True" EnableVirtualScrollPaging="True" ScrollHeight="400px" />
            <Resizing AllowColumnResize="True" />
            <ClientEvents OnFilterMenuShowing="filterMenuShowing" />
        </ClientSettings>
        <MasterTableView AutoGenerateColumns="False" DataKeyNames="RULE_ID" DataSourceID="BasWareData">
            <Columns>
                <telerik:GridBoundColumn AllowFiltering="False" DataField="RULE_ID" DataType="System.Decimal" FilterControlAltText="Filter RULE_ID column" HeaderText="RULE ID" ReadOnly="True" SortExpression="RULE_ID" UniqueName="RULE_ID">
                </telerik:GridBoundColumn>
 
                <telerik:GridBoundColumn DataField="COMP_NO" FilterControlAltText="Filter COMP_NO column" HeaderText="COMP NO" SortExpression="COMP_NO" UniqueName="COMP_NO">
                
                <FilterTemplate>
                    <telerik:RadDropDownList 
                        id="RadDropDownList1"
                        DataSourceID="BasWareDataCOMP"
                        DataTextField="COMP_NO"
                        AutoPostBack="true"
                        DropDownHeight="300px"
                        DropDownWidth="110px"
                        DataValueField="COMP_NO" 
                        runat="server"
                        OnItemDataBound="RadDropDownList1_DataBound"
                        Width="100%"
                        OnItemSelected="RadDropDownList1_ItemSelected"
                        >
                    </telerik:RadDropDownList>
                      
                </FilterTemplate>
                 
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="SUPPLIER_NUM" FilterControlAltText="Filter SUPPLIER_NUM column" HeaderText="SUPPLIER NUM" SortExpression="SUPPLIER_NUM" UniqueName="SUPPLIER_NUM">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="INVOICE_CURRENCY" FilterControlAltText="Filter INVOICE_CURRENCY column" HeaderText="INVOICE CURRENCY" SortExpression="INVOICE_CURRENCY" UniqueName="INVOICE_CURRENCY">
                
                <FilterTemplate>
                    <telerik:RadDropDownList
                        ID="RadDropDownList2"
                        runat="server"
                        DataSourceID="BasWareDataCURRENCY"
                        DataTextField="INVOICE_CURRENCY"
                        AutoPostBack="true"
                        DropDownHeight="150px"
                        DropDownWidth="110px"
                        DataValueField="INVOICE_CURRENCY"     
                        OnItemDataBound="RadDropDownList2_DataBound"
                        Width="100%"
                        OnItemSelected="RadDropDownList2_ItemSelected">
                    </telerik:RadDropDownList>
                </FilterTemplate>
 
 
protected void RadGrid1_PreRender(object sender, EventArgs e)
        {
            foreach (GridFilteringItem filterItem in RadGrid1.MasterTableView.GetItems(GridItemType.FilteringItem))
            {
                DropDownList dropdown1 = (DropDownList)filterItem.FindControl("RadDropDownList1");
                if (Session["slectedValue"] != null) dropdown1.SelectedIndex = Convert.ToInt32(Session["slectedValue"]);
                DropDownList dropdown2 = (DropDownList)filterItem.FindControl("RadDropDownList2");
                if (Session["slectedValue"] != null) dropdown2.SelectedIndex = Convert.ToInt32(Session["slectedValue"]);
            }
        }
 
        protected void RadDropDownList1_ItemSelected(object sender, DropDownListEventArgs e)
        {
            if (e.Index == _compIndex) return;
 
            if (e.Index == 0) _compFilter = "";
            else
            {
                _compFilter = "([COMP_NO] = '" + e.Text + "')";
                _compIndex = e.Index;
            }
            concatFilterExpression();
        }
 
        protected void RadDropDownList2_ItemSelected(object sender, DropDownListEventArgs e)
        {
            if (e.Index == _invoiceIndex) return;
 
            if (e.Text == _dropDownDefault) _invoiceCurrencyFilter = "";
            else
            {
                _invoiceCurrencyFilter = "([INVOICE_CURRENCY] = '" + e.Text + "')";
                _invoiceIndex = e.Index;
            }
            concatFilterExpression();
        }
 
        private void concatFilterExpression()
        {
            if (_compFilter == "" || _invoiceCurrencyFilter == "") _currentFilterExpression = String.Concat(_compFilter, _invoiceCurrencyFilter);
            else _currentFilterExpression = String.Concat(_compFilter, " AND ", _invoiceCurrencyFilter);
            RadGrid1.MasterTableView.FilterExpression = _currentFilterExpression;          
            RadGrid1.Rebind();
             
            Label2.Text = _currentFilterExpression;
            Label4.Text = _compIndex.ToString();
        }

0
IT-Fjárvakur
Top achievements
Rank 1
answered on 31 Jul 2015, 12:33 PM
Ok it has something to do with the autoPostBack=true. Some part of the batabinding should be in the page_Load function under if(!pagePostBack) something. But The strange thing is I cant access the dropdownlist, I cant fint it in the global scope of the .cs file and when I try to access them in the prerender, I get that Telerik/System dropdownlist/RadDropDownlist
0
Eyup
Telerik team
answered on 05 Aug 2015, 07:12 AM
Hi Óttar,

Please note that modifying the FilterExpression is quite tricky and it can be error-prone, therefore, it is not recommended. You can implement the approach demonstrated in the following approach to achieve this requirement:
http://demos.telerik.com/aspnet-ajax/grid/examples/functionality/filtering/filter-templates/defaultcs.aspx

In addition, I am sending 3 samples demonstrating:

1. Server-side binding of the combo using its DataBinding event handler.
2. Programmatic creation of the entire FilterTemplate.
3. Multi-value combo searching.

Please run and examine the provided web sites and let me know if they help you.


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
0
IT-Fjárvakur
Top achievements
Rank 1
answered on 05 Aug 2015, 05:39 PM

Thank you, unfortunately I wrote my own multi custom filter to suit my needs and discarding the default one. I do not know if these examples are any good to me now, but if I'll have some trouble later on I will definitely check them out.

0
Eyup
Telerik team
answered on 07 Aug 2015, 02:11 PM
Hello Óttar,

Please take your time to examine the samples and feel free to turn to us if new questions arise.
Also, please have in mind that creating custom controls is out of our support scope, even if they are inherited directly from Telerik controls.

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
0
Alan
Top achievements
Rank 1
answered on 23 Jan 2020, 03:00 PM

Hi Eyup,

Thank you for taking the time to present the 3 additional samples. I have been searching online with no luck on how to filter a RadGrid containing a Combo with checkboxes (sample #3). Works very well. 

Grid filtering is such an important feature for RadGrid. Will support for sample #3 example and perhaps a multiple search button be included in upcoming releases?

Thanks.

0
Eyup
Telerik team
answered on 28 Jan 2020, 06:57 AM

Hello Alan,

 

I'm glad the provided solution have proven helpful.

The grid provides built-in Excel-like filtering:
https://demos.telerik.com/aspnet-ajax/grid/examples/functionality/filtering/excel-like-filtering/defaultcs.aspx

If you have a different idea you want to see implemented built-in, feel free to share it in our Public Portal:
https://feedback.telerik.com/aspnet-ajax

 

Regards,
Eyup
Progress Telerik

Get quickly onboarded and successful with UI for ASP.NET AJAX with the Virtual Classroom technical trainings, available to all active customers. Learn More.
0
Alan
Top achievements
Rank 1
answered on 29 Jan 2020, 03:08 PM

Hi Eyup,

I submitted a feature: https://feedback.telerik.com/aspnet-ajax/1451365-final-filter-button

Al

Tags
Grid
Asked by
Carl
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Carl
Top achievements
Rank 1
IT-Fjárvakur
Top achievements
Rank 1
Eyup
Telerik team
Alan
Top achievements
Rank 1
Share this question
or