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

8 posts, 1 answers
  1. Carl
    Carl avatar
    195 posts
    Member since:
    Oct 2012

    Posted 17 Apr 2009 Link to this post

    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.

  2. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 17 Apr 2009 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Carl
    Carl avatar
    195 posts
    Member since:
    Oct 2012

    Posted 17 Apr 2009 Link to this post

    Thanks for your help. The PreRender event worked great.
  5. Óttar
    Óttar avatar
    5 posts
    Member since:
    Jul 2015

    Posted 31 Jul 2015 Link to this post

    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();
            }

  6. Óttar
    Óttar avatar
    5 posts
    Member since:
    Jul 2015

    Posted 31 Jul 2015 in reply to Óttar Link to this post

    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
  7. Eyup
    Admin
    Eyup avatar
    3015 posts

    Posted 05 Aug 2015 Link to this post

    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
  8. Óttar
    Óttar avatar
    5 posts
    Member since:
    Jul 2015

    Posted 05 Aug 2015 in reply to Eyup Link to this post

    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.

  9. Eyup
    Admin
    Eyup avatar
    3015 posts

    Posted 07 Aug 2015 Link to this post

    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
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017