1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Pre 5.4 code example on using choice field to filter grid in FrontEnd UserControl

Pre 5.4 code example on using choice field to filter grid in FrontEnd UserControl

4 posts, 0 answered
  1. Markus
    Markus avatar
    2763 posts
    Registered:
    25 Nov 2005
    22 Mar 2013
    Link to this post
    Hello everybody

    1. All credit to this post go to Jen Peleva. 
    2. I think it might be very common that you create a module in module builder and have a choice field which you want to use later to filter the results.

    Please note that pre 5.3 it seems choicefields were of type string[] and later ChoiceOption so this code is for pre 5.4 modules. We wasted some time because I importat that module to an 5.4 version.

    The idea is the following in my example.

    You have employees (German Mitarbeiter) and want to filter them by function (German Funktion)

    See the working example here on this demo site:  http://schoefflisdorf.ch.mserver2.arvixevps.com/verwaltung/personenregister

    Well I have an ascx which has the following code: 

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="mitarbeiter_liste.ascx.cs" Inherits="SitefinityWebApp.UserControls.mitarbeiter_liste" %>
    <br />
    <table>
    <tr><td>Filtern Sie hier: </td><td><telerik:RadComboBox ID="RadComboBox2" runat="server"
         AutoPostBack="true"
        Width="250px"  OnDataBound="RadComboBox2_DataBound" >
    </telerik:RadComboBox></td></tr></table>
     
    <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" CellSpacing="0" GridLines="None" AllowSorting="True" ShowGroupPanel="False" Skin="Metro"
                     onneeddatasource="RadGrid1_NeedDataSource"    >
        <ClientSettings AllowDragToGroup="True">
        </ClientSettings>
        <MasterTableView  >
            <CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>
     
            <RowIndicatorColumn Visible="True" FilterControlAltText="Filter RowIndicator column">
                <HeaderStyle Width="20px"></HeaderStyle>
            </RowIndicatorColumn>
     
            <ExpandCollapseColumn Visible="True" FilterControlAltText="Filter ExpandColumn column">
                <HeaderStyle Width="20px"></HeaderStyle>
            </ExpandCollapseColumn>
     
            <Columns>
                <telerik:GridBoundColumn DataField="Nachname" FilterControlAltText="Nachname" HeaderText="Nachname" UniqueName="Nachname">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Vorname" FilterControlAltText="Vorname" HeaderText="Vorname" UniqueName="Vorname">
                </telerik:GridBoundColumn>
     
                <telerik:GridBoundColumn DataField="Telefon" FilterControlAltText="Telefon" HeaderText="Telefon" UniqueName="Telefon">
                </telerik:GridBoundColumn>
     <%--           <telerik:GridBoundColumn DataField="Mail" FilterControlAltText="Email" HeaderText="E-Mail" UniqueName="Email">
                </telerik:GridBoundColumn>--%>
                <telerik:GridHyperLinkColumn   FilterControlAltText="Email" HeaderText="E-Mail" UniqueName="Email" DataNavigateUrlFormatString="mailto:{0}" DataTextField="Mail" DataNavigateUrlFields="Mail" Target="_blank"></telerik:GridHyperLinkColumn>
       
              
                <telerik:GridBoundColumn DataField="Taetigkeit" FilterControlAltText="Taetigkeit" HeaderText="Tätigkeit" UniqueName="Taetigkeit">
                </telerik:GridBoundColumn>
            </Columns>
     
            <EditFormSettings>
                <EditColumn FilterControlAltText="Filter EditCommandColumn column"></EditColumn>
            </EditFormSettings>
        </MasterTableView>
     
        <FilterMenu EnableImageSprites="False"></FilterMenu>
    </telerik:RadGrid>

    In my codebehind I am doing the following (and yes there are probably better ways to do so so please don't by shy to post back)

    1) Pull all the used Fuktionen of Mitarbeiter (Functions of employees). I only want the once used. Kind of A, D, R, S and not A-Z where a client would have a bad experiance when trying F.
    2) Sort them alphabetically
    3) have it distict 
    4) Bind it to my RadComboBox

        public void RetrieveCollectionOfFunktionen()
         {
     
          DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();
          Type mitarbeiterType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.Mitarbeiter.Mitarbeiter");
     
          List<DynamicContent> dataItems = dynamicModuleManager.GetDataItems(mitarbeiterType).Where(s => s.Status == ContentLifecycleStatus.Live).ToList();
        List<string> funktionDropdownDataSource = new List<string>();
       foreach (var dataItem in dataItems)
          {
              var funktionValue = dataItem.GetValue<string[]>("Funktion");
     
             funktionDropdownDataSource.AddRange(funktionValue);
         }
         //order the results ASC (my choiceFiled name si "Fruits")
    //   var dataSourceASC = funktionDropdownDataSource.OrderBy(x => x.FieldValue<string>("Funktion"));
       var dataSourceASC = funktionDropdownDataSource.OrderBy(x => x);
          //distinct the results
       var distinctSelection = dataSourceASC.Distinct();
        RadComboBox2.DataSource = distinctSelection;
     
             RadComboBox2.DataBind();
     
         }

    I do add a first entry to the RadCombobox that reads 'alle Einträge' (all entries)

    protected void RadComboBox2_DataBound(object sender, EventArgs e)
     {
         var combo = (RadComboBox)sender;
         combo.Items.Insert(0, new RadComboBoxItem("Alle Einträge", string.Empty));
     }

    On page load I do fill the RadCombobox and the Grid. On postback I filter the grid

    protected void Page_Load(object sender, EventArgs e)
           {
     
    //we are getting kategorien für filter
                   RetrieveCollectionOfFunktionen();
     
               if (!Page.IsPostBack)
               {
                                  
                   // Fetch a collection of "live" and "visible" mitarbeiter items.
                   var myCollection = GetDataItems();
     
                   // Binds the collection of Person items to the RadGrid
                   RadGrid1.DataSource = myCollection;
                   RadGrid1.DataBind();
               }
               else
               {
                  
                   // Fetch a collection of "live" and "visible" mitarbeiter items.
                   var myCollection = GetDataItemsFiltered();
     
                   // Binds the collection of Person items to the RadGrid
                   RadGrid1.DataSource = myCollection;
                   RadGrid1.DataBind();
               }
                
           }

    The RadCombobox does an autopostback (no ajax I am happy it runs like this) and these are my two ways to get the employees. One is unfiltered the other filterd. If the first combobox value 'all entries'  'alle einträge' is selected then the filtered is empty so I display all entires.

    // Gets a collection of "live" and "visible" mitarbeiter items.
           public IQueryable<DynamicContent> GetDataItems()
           {
               DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();
               Type mitarbeiterType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.Mitarbeiter.Mitarbeiter");
     
               // Fetch a collection of "live" and "visible" mitarbeiter items.
               var myCollection = dynamicModuleManager.GetDataItems(mitarbeiterType).Where(i => i.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live && i.Visible == true).OrderBy(i => i.FieldValue<string>("Nachname"));
     
               return myCollection;
           }
     
     
           // Gets a filtered collection of "live" and "visible" mitarbeiter items.
           public IQueryable<DynamicContent> GetDataItemsFiltered()
           {
               DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();
               Type mitarbeiterType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.Mitarbeiter.Mitarbeiter");
     
               // Fetch a collection of "live" and "visible" mitarbeiter items.
               var myCollection = dynamicModuleManager.GetDataItems(mitarbeiterType).Where((i => i.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live && i.Visible == true && i.FieldValue<string[]>("Funktion").Contains(RadComboBox2.SelectedValue.ToString()))).OrderBy(i => i.FieldValue<string>("Nachname"));
     
               if (myCollection.Count() == 0)
               {
              var myNewCollection =  GetDataItems();
              return myNewCollection;
               }
               else
               {
               return myCollection;
               }
           }
     
           protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
           {
               // Fetch a collection of "live" and "visible" mitarbeiter items.
               var myCollection = GetDataItems();
     
               // Binds the collection of Person items to the RadGrid
               RadGrid1.DataSource = myCollection;
              
           }


    Again credits go to Jen and this example will work with modules created pre 5.4 only. I did ask her if this would make a KB article (which I think it should) and ask her if they make one that they will have an 5.4 module code example as well.

    Hope this can be of help to you out there.

    Markus
  2. Victor Velev
    Victor Velev avatar
    1345 posts
    Registered:
    06 Jul 2015
    27 Mar 2013
    Link to this post
    Hi Markus,

    Thank you for sharing the example with the community. I am sure that it will be useful to a lot of users.

    All the best,
    Victor Velev
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Markus
    Markus avatar
    2763 posts
    Registered:
    25 Nov 2005
    27 Mar 2013 in reply to Victor Velev
    Link to this post
    Dear Victor

    As mentioned 

    a) credit goes to Jen from telerik
    b) I think this is very common. I would love to see this as a KB article for pre 5.4 and 5.4 + code  with link in the module builder code reference.

    Markus
  4. Victor Velev
    Victor Velev avatar
    1345 posts
    Registered:
    06 Jul 2015
    29 Mar 2013
    Link to this post
    Hello Makrus,

    We will consider creating an article about the topic as soon as possible.

    Thank you for feedback!

    Regards,
    Victor Velev
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
4 posts, 0 answered