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

Forums / Developing with Sitefinity / Adding Search User Control To LibraryListView

Adding Search User Control To LibraryListView

10 posts, 0 answered
  1. heads
    heads avatar
    6 posts
    Registered:
    30 Aug 2006
    26 Oct 2010
    Link to this post
    I am trying to create a Search User Control to the LibraryListView as per attached image.

    I've mapped the view to an external template using the perscribed method.

    The code from the user control looks like this:

    001.public partial class LibrarySearch : System.Web.UI.UserControl
    002.    {
    003.        public Repeater LibrariesRepeater
    004.        {
    005.            get
    006.            {
    007.                return Page.FindControlRecursive<Repeater>("librariesRepeater");
    008.            }
    009.        }
    010. 
    011.        public GridView LibrariesGrid { get { return Page.FindControlRecursive<GridView>("librariesGrid"); } }
    012. 
    013.        protected override object SaveViewState()
    014.        {
    015.            ViewState.Add("searchField", searchField.SelectedValue);
    016.            ViewState.Add("searchTerm", searchTerm.Text);
    017.            return base.SaveViewState();
    018.        }
    019. 
    020.        protected override void LoadViewState(object savedState)
    021.        {
    022.            base.LoadViewState(savedState);
    023. 
    024.            searchField.SelectedValue = Convert.ToString(ViewState["searchField"] ?? searchField.Items[0].Value);
    025.            searchTerm.Text = Convert.ToString(ViewState["searchTerm"] ?? searchTerm.Text);
    026.        }
    027. 
    028.        protected void Page_Load
    029.            (
    030.            object sender,
    031.            EventArgs e)
    032.        {
    033.            var selector = ContentSelectorFactory.Create(ContentType.Library);
    034.            var items = selector.GetSearchableMetaKeys()
    035.                .Select(info => new ListItem(info.Caption, info.Key))
    036.                .ToArray();
    037. 
    038.            searchField.Items.Clear();
    039.            searchField.Items.AddRange(items);
    040. 
    041.            Page.FindControlRecursive<Pager>("pager1").SelectedPageChanged += pager_SelectedPageChanged;
    042.            Page.FindControlRecursive<Pager>("pager2").SelectedPageChanged += pager_SelectedPageChanged;
    043.        }
    044. 
    045.        protected override void CreateChildControls()
    046.        {
    047.            ReplaceObjectDataSource();
    048. 
    049.            base.CreateChildControls();
    050.        }
    051. 
    052.        private void ReplaceObjectDataSource()
    053.        {
    054.            var objectDataSource = Page.FindControlRecursive<ObjectDataSource>("LibrariesDataSource");
    055.            objectDataSource.TypeName = typeof(LibraryManagerExtension).FullName;
    056.            objectDataSource.SelectMethod = "GetAllLibraries";
    057.            objectDataSource.SelectCountMethod = "GetFilteredTotal";
    058.            objectDataSource.MaximumRowsParameterName = "max";
    059.            objectDataSource.StartRowIndexParameterName = "from";
    060.            objectDataSource.SortParameterName = "sortExp";
    061.            objectDataSource.EnablePaging = true;
    062. 
    063.            objectDataSource.ObjectCreating += objectDataSource_ObjectCreating;
    064.            objectDataSource.Selecting += objectDataSource_Selecting;
    065.            objectDataSource.Selected += objectDataSource_Selected;
    066.            objectDataSource.SelectParameters.Clear();
    067.            objectDataSource.SelectParameters.Add("fieldName", searchField.SelectedValue);
    068.            objectDataSource.SelectParameters.Add("fieldValue", searchTerm.Text);
    069. 
    070.        }
    071. 
    072.        protected void search_Click
    073.            (
    074.            object sender,
    075.            EventArgs e)
    076.        {
    077.            DisplaySearchResults(1);
    078.        }
    079. 
    080.        private void objectDataSource_Selected
    081.            (
    082.            object sender,
    083.            ObjectDataSourceStatusEventArgs e)
    084.        {
    085.            if (!(e.ReturnValue is int)) return;
    086.            var count = Convert.ToDouble(e.ReturnValue);
    087. 
    088.            CreatePaging(count);
    089.        }
    090. 
    091. 
    092.        private void CreatePaging
    093.            (
    094.            double count)
    095.        {
    096. 
    097.            var pager1 = Page.FindControlRecursive<Pager>("pager1");
    098.            var pager2 = Page.FindControlRecursive<Pager>("pager2");
    099. 
    100.            ConfigurePager(pager1, pager1.SelectedPage, count, 50);
    101.            ConfigurePager(pager2, pager2.SelectedPage, count, 50);
    102.        }
    103. 
    104.        private static void ConfigurePager
    105.            (
    106.            Pager pager,
    107.            int currentPage,
    108.            double count,
    109.            int itemsPerPage)
    110.        {
    111.            pager.SelectedPage = currentPage;
    112.            pager.PageCount = (int)Math.Ceiling(count / itemsPerPage);
    113.            pager.Visible = true;
    114.        }
    115. 
    116.        private static void objectDataSource_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
    117.        {
    118.            e.ObjectInstance = new LibraryManagerExtension();
    119.        }
    120. 
    121.        private void pager_SelectedPageChanged
    122.            (
    123.            object sender,
    124.            EventArgs e)
    125.        {
    126.            var currentPage = ((Pager)sender).SelectedPage;
    127.            DisplaySearchResults(currentPage);
    128.        }
    129. 
    130.        private void objectDataSource_Selecting
    131.            (
    132.            object sender,
    133.            ObjectDataSourceSelectingEventArgs e)
    134.        {
    135.            e.InputParameters["fieldName"] = searchField.SelectedValue;
    136.            e.InputParameters["fieldValue"] = searchTerm.Text;
    137.            e.Arguments.RetrieveTotalRowCount = true;
    138.        }
    139. 
    140.        private void DisplaySearchResults(int currentPage)
    141.        {
    142.            if (LibrariesRepeater != null)
    143.            {
    144.                LibrariesRepeater.DataBind();
    145.            }
    146. 
    147.            if (LibrariesGrid == null) return;
    148.            LibrariesGrid.DataBind();
    149.        }
    150.    }


    I've also super classed the LibraryManager to look like this.

    public class LibraryManagerExtension : LibraryManager
        {
            public IList GetAllLibraries
                (
                int from,
                int max,
                string sortExp,
                string fieldName,
                string fieldValue )
            {
                var selector = ContentSelectorFactory.Create(ContentType.Library);
                selector.AddFilter(fieldName, fieldValue, SearchCondition.Like, JoinType.And);
     
                return selector
                    .GetContent()
                    .Cast<LibraryContent>()
                    .Select(result => result.Library)
                    .Skip(from)
                    .Take(max)
                    .ToList();
            }
     
            public int GetFilteredTotal
                (
                string fieldName,
                string fieldValue )
            {
                var selector = ContentSelectorFactory.Create(ContentType.Library);
                selector.AddFilter(fieldName, fieldValue, SearchCondition.Like, JoinType.And);
     
                return selector.GetContent().Cast<LibraryContent>().Select(result => result.Library).Count();
            }
        }

    When the page loads (no postback) I get the list of libraries as expected but when I attempt to search by criteria on postback I get the following error:

    Exception Type: System.InvalidOperationException
     
    Message: ObjectDataSource 'LibrariesDataSource' could not find a non-generic method 'GetAllLibraries' that has parameters: fieldName, fieldValue, sortExp, max, from.
     
    Source: System.Web
     
    Stack Trace:
       at System.Web.UI.WebControls.ObjectDataSourceView.GetResolvedMethodData(Type type, String methodName, IDictionary allParameters, DataSourceOperation operation)
       at System.Web.UI.WebControls.ObjectDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments)
       at System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback)
       at System.Web.UI.WebControls.DataBoundControl.PerformSelect()
       at Telerik.Web.UI.GridTableView.PerformSelect()
       at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()
       at Telerik.Web.UI.GridTableView.DataBind()
       at Telerik.Web.UI.RadGrid.DataBind()
       at System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound()
       at Telerik.Web.UI.RadGrid.OnLoad(EventArgs e)
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
    ------------------------------------------------------------------------------------

    Even if I move the ReplaceObjectDataSource() call into the Page_Init event I get the same error.

    I was wondering if someone could help.

    Also I am trying to get a reference to the LibraryList control on the page, as I was able to do under 3.5 but I can't access it.

    Any and all help will be much appreciated.

    Regards

    Craig
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    26 Oct 2010
    Link to this post
    Hello heads,

    You have to inherit from the base view - Telerik.Libraries.WebControls.Admin.LibraryList or just implement another control - Repeater or RadGrid that will show the filtered results, since the default Repeater or RadGrid will remain not filtered. You have to override InitializeControls of the base class and there move the logic you have added inside ReplaceObjectDataSource() or use LibraryManger methods



    • public ILibrary GetLibrary(Guid libraryID)
    • public ILibrary GetLibrary(string name)
    • public IList GetAllLibraries()
    • public IList GetAllLibraries(string sortExp)
    • public IList GetAllLibraries(int from, int max, string sortExp)
    • public IList GetAllLibraries(string typeName, bool isEqual)
    • public IList GetAllLibraries(int from, int max, string sortExp, string typeName, bool isEqual)>
    • public int GetLibrariesCount()

    Regards,
    Ivan Dimitrov
    the Telerik team
    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 Public Issue Tracking system and vote to affect the priority of the items
  3. heads
    heads avatar
    6 posts
    Registered:
    30 Aug 2006
    26 Oct 2010
    Link to this post
    Hi Ivan

    Thanks for the quick reply.

    I keep forgetting about the new architecture due to the initial product being developed under 3.5.

    I'll see how I go.

    Regards

    Craig
  4. Craig Taylor
    Craig Taylor avatar
    10 posts
    Registered:
    22 Apr 2010
    20 May 2011
    Link to this post
    I've followed the above I've created a custom class
    using System;
    using System.Collections;
    using System.Linq;
    using System.Web.UI.WebControls;
    using Telerik.Cms.Web.UI;
    using Telerik.Libraries.WebControls.Admin;
    using WorldVision.Sitefinity.Data.Data;
    using WorldVision.Sitefinity.Data.Data.Linq;
     
    namespace WorldVision.Sitefinity.Modules.Extensions.Libraries
    {
        public class WvaLibrariesList : LibraryList<LibrariesView>
        {
            public TextBox SearchTerm
            {
                get
                {
                    return Page.FindControlRecursive<TextBox>("searchTerm");
                }
            }
     
            public DropDownList SearchField
            {
                get
                {
                    return Page.FindControlRecursive<DropDownList>("searchField");
                }
            }
     
            public Button Search
            {
                get
                {
                    return Page.FindControlRecursive<Button>("search");
                }
            }
     
            protected override void InitializeControls(System.Web.UI.Control viewContainer)
            {
                Search.Click += search_Click;
     
                var objectDataSource = Page.FindControlRecursive<ObjectDataSource>("LibrariesDataSource");
                //objectDataSource.TypeName = typeof(LibraryManagerExtension).FullName;
                objectDataSource.SelectMethod = "GetAllLibraries";
                objectDataSource.SelectCountMethod = "GetFilteredTotal";
                objectDataSource.MaximumRowsParameterName = "max";
                objectDataSource.StartRowIndexParameterName = "from";
                objectDataSource.SortParameterName = "sortExp";
                objectDataSource.EnablePaging = true;
     
                //objectDataSource.ObjectCreating += objectDataSource_ObjectCreating;
                objectDataSource.Selecting += objectDataSource_Selecting;
                objectDataSource.Selected += objectDataSource_Selected;
                objectDataSource.SelectParameters.Clear();
                objectDataSource.SelectParameters.Add("fieldName", SearchField.SelectedValue);
                objectDataSource.SelectParameters.Add("fieldValue", SearchTerm.Text);
     
                base.InitializeControls(viewContainer);
            }
     
            private void objectDataSource_Selected
                (
                object sender,
                ObjectDataSourceStatusEventArgs e)
            {
                if (!(e.ReturnValue is int)) return;
                var count = Convert.ToDouble(e.ReturnValue);
     
                CreatePaging(count);
            }
     
            public Repeater LibrariesRepeater
            {
                get
                {
                    return Page.FindControlRecursive<Repeater>("librariesRepeater");
                }
            }
     
            public GridView LibrariesGrid { get { return Page.FindControlRecursive<GridView>("librariesGrid"); } }
            public Pager TopPager { get { return Page.FindControlRecursive<Pager>("TopPager"); } }
            public Pager BottomPager { get { return Page.FindControlRecursive<Pager>("BottomPager"); } }
     
            protected override object SaveViewState()
            {
                ViewState.Add("SearchField", SearchField.SelectedValue);
                ViewState.Add("SearchTerm", SearchTerm.Text);
                return base.SaveViewState();
            }
     
            protected override void LoadViewState(object savedState)
            {
                base.LoadViewState(savedState);
     
                SearchField.SelectedValue = Convert.ToString(ViewState["SearchField"] ?? SearchField.Items[0].Value);
                SearchTerm.Text = Convert.ToString(ViewState["SearchTerm"] ?? SearchTerm.Text);
            }
     
            protected void Page_Load
                (
                object sender,
                EventArgs e)
            {
                var selector = ContentSelectorFactory.Create(ContentType.Library);
                var items = selector.GetSearchableMetaKeys()
                    .Select(info => new ListItem(info.Caption, info.Key))
                    .ToArray();
     
                SearchField.Items.Clear();
                SearchField.Items.AddRange(items);
     
                Page.FindControlRecursive<Pager>("pager1").SelectedPageChanged += pager_SelectedPageChanged;
                Page.FindControlRecursive<Pager>("pager2").SelectedPageChanged += pager_SelectedPageChanged;
     
                TopPager.SelectedPageChanged += pager_SelectedPageChanged;
                BottomPager.SelectedPageChanged += pager_SelectedPageChanged;
            }
     
            protected void search_Click
                (
                object sender,
                EventArgs e)
            {
                SetCurrentPage(1);
                DisplaySearchResults();
            }
     
     
            private void CreatePaging
                (
                double count)
            {
                ConfigurePager(TopPager, CurrentPage, count);
                ConfigurePager(BottomPager, CurrentPage, count);
            }
     
            protected int CurrentPage { get; set; }
     
     
            private static void ConfigurePager
                (
                Pager pager,
                int currentPage,
                double count)
            {
                pager.SelectedPage = currentPage;
                pager.PageCount = (int)Math.Ceiling(count / 50);
                pager.Visible = true;
            }
     
     
            private void pager_SelectedPageChanged
                (
                object sender,
                EventArgs e)
            {
                var currentPage = ((Pager)sender).SelectedPage;
     
                SetCurrentPage(currentPage);
                DisplaySearchResults();
            }
     
            private void SetCurrentPage(int currentPage)
            {
                CurrentPage = currentPage;
            }
     
            private void objectDataSource_Selecting
                (
                object sender,
                ObjectDataSourceSelectingEventArgs e)
            {
                e.InputParameters["fieldName"] = SearchField.SelectedValue;
                e.InputParameters["fieldValue"] = SearchTerm.Text;
                e.Arguments.RetrieveTotalRowCount = true;
            }
     
            private void DisplaySearchResults()
            {
                if (LibrariesRepeater != null)
                {
                    LibrariesRepeater.DataSource = GetFilteredDataSource();
                    LibrariesRepeater.DataSourceID = null;
                    LibrariesRepeater.DataBind();
                }
     
     
                if (LibrariesGrid == null) return;
                LibrariesGrid.DataSourceID = null;
                LibrariesGrid.DataSource = GetFilteredDataSource();
                LibrariesGrid.DataBind();
            }
     
            private IList GetFilteredDataSource()
            {
                var pager1 = Page.FindControlRecursive<Pager>("pager1");
                var pager2 = Page.FindControlRecursive<Pager>("pager2");
                pager1.Visible = pager2.Visible = false;
                return new LibraryManagerExtension().GetAllLibraries(SearchField.SelectedValue, SearchTerm.Text.Trim(), "asc", pager1.PageCount, pager1.SelectedPage);
            }
        }
    }

    In the Telerik.Sitefinity.Configuration.ControlsConfig.xml I've modified the following
    <viewSettings hostType="Telerik.Libraries.WebControls.Admin.LibraryList`1[[Telerik.Libraries.WebControls.Admin.LibrariesView, Telerik.Libraries]]" layoutTemplatePath="~/Sitefinity/Admin/ControlTemplates/Libraries/LibraryListView.ascx">
        <views>
            <remove name="LibraryList`1" />
            <add name="LibraryList`1" viewType="WorldVision.Sitefinity.Modules.Extensions.Libraries.WvaLibrariesList, WorldVision.Sitefinity.Modules"></add>
        </views>
    </viewSettings>

    but when I go to the Libraries Module page the custom class isn't used. The page loads fine but it doesn't use my created class.

    Ideas?
  5. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    20 May 2011
    Link to this post
    Hi Craig,

    Have you restarted the application after the controls config changes, because until then they will not apply.

    Kind regards,
    Ivan Dimitrov
    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
  6. Craig Taylor
    Craig Taylor avatar
    10 posts
    Registered:
    22 Apr 2010
    20 May 2011
    Link to this post
    I have restarted and still the new code is not hit
  7. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    24 May 2011
    Link to this post
    Hi Craig ,

    Try using the following mapping



    <viewSettings hostType="Telerik.Libraries.WebControls.Admin.LibrariesView, Telerik.Libraries">
      <views>
        <remove name="LibraryList`1"></remove>
        <add name="LibraryList`1" viewType="Telerik.Sitefinity.Samples.TestLibraryListView, App_Code"></add>
      </views>
    </viewSettings>



    Here is the test class

    namespace Telerik.Sitefinity.Samples
    {
        public class TestLibraryListView : LibraryList<LibrariesView>
        {
            public TestLibraryListView()
            {
                //
                // TODO: Add constructor logic here
                //
            }
     
            protected override void InitializeControls(System.Web.UI.Control viewContainer)
            {
                base.InitializeControls(viewContainer);
            }
     
        }
    }


    Regards,
    Ivan Dimitrov
    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
  8. Craig Taylor
    Craig Taylor avatar
    10 posts
    Registered:
    22 Apr 2010
    10 Jun 2011
    Link to this post
    When I do that I get the stack trace:

    Exception Type: System.ArgumentException

    Message: Invalid resource name (Telerik.Libraries.Resources.ControlTemplates.Backend.LibraryListView.ascx) for assembly (App_Code.i159zvhe, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null) or empty template.

    Source: Telerik.Cms.Web.UI

    Stack Trace:
       at Telerik.Framework.Web.ControlUtils.GetTemplate(String virtualPath, String resourceFileName, Type assemblyInfo, Type localizationAssemblyInfo)
       at Telerik.Framework.Web.ControlUtils.GetTemplate(String virtualPath, String resourceFileName, Type assemblyInfo, Type localizationAssemblyInfo, Type templateMapType, String additionalTemplateKey)
       at Telerik.Cms.Web.UI.ViewModeControl`1.CreateLayoutTemplate(Type assemblyInfo, Type localizationAssemblyInfo)
       at Telerik.Cms.Web.UI.ViewModeControl`1.get_LayoutTemplate()
       at Telerik.Cms.Web.UI.ViewModeControl`1.get_Container()
       at Telerik.Cms.Web.UI.ViewModeControl`1.CreateChildControls()
       at System.Web.UI.Control.EnsureChildControls()
       at System.Web.UI.Control.PreRenderRecursiveInternal()
       at System.Web.UI.Control.PreRenderRecursiveInternal()
       at System.Web.UI.Control.PreRenderRecursiveInternal()
       at System.Web.UI.Control.PreRenderRecursiveInternal()
       at System.Web.UI.Control.PreRenderRecursiveInternal()
       at System.Web.UI.Control.PreRenderRecursiveInternal()
       at System.Web.UI.Control.PreRenderRecursiveInternal()
       at System.Web.UI.Control.PreRenderRecursiveInternal()
       at System.Web.UI.Control.PreRenderRecursiveInternal()
       at System.Web.UI.Control.PreRenderRecursiveInternal()
       at System.Web.UI.Control.PreRenderRecursiveInternal()
       at System.Web.UI.Control.PreRenderRecursiveInternal()
       at System.Web.UI.Control.PreRenderRecursiveInternal()
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  9. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    13 Jun 2011
    Link to this post
    Hello Craig,


    It looks like you are trying to get the default backend template. Please override the LayoutTemplatePath property and set the template path to a location of your project. You can download the external templates from where you can get the default LibraryListView.ascx and use it.

    Regards,
    Ivan Dimitrov
    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
  10. Craig Taylor
    Craig Taylor avatar
    10 posts
    Registered:
    22 Apr 2010
    03 Jan 2012
    Link to this post
    That worked thank you
Register for webinar
10 posts, 0 answered