Sorting,Paging and Search not working in Custom Folder

For general questions and discussions specific to the AbleCommerce 7.0 Asp.Net product.
Post Reply
waltersnowslin
Ensign (ENS)
Ensign (ENS)
Posts: 19
Joined: Wed Jul 15, 2009 2:58 am

Sorting,Paging and Search not working in Custom Folder

Post by waltersnowslin » Wed Oct 14, 2009 2:09 am

Hi

I have duplicated few UserControls say, CategoryGridPage, SearchPage for my store in Custom Folder. What I now encounter is the sorting, paging and search (Other products of Manufacturer's) not working. Can anyone please aid me in this regard..

Thanks
Walter Snowslin

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: Sorting,Paging and Search not working in Custom Folder

Post by mazhar » Wed Oct 14, 2009 3:11 am

Most probably you made some change that is breaking these things.

waltersnowslin
Ensign (ENS)
Ensign (ENS)
Posts: 19
Joined: Wed Jul 15, 2009 2:58 am

Re: Sorting,Paging and Search not working in Custom Folder

Post by waltersnowslin » Wed Oct 14, 2009 4:25 am

Hi

If I give you the code can you jus review it and spot it out????


Thanks
Walter

waltersnowslin
Ensign (ENS)
Ensign (ENS)
Posts: 19
Joined: Wed Jul 15, 2009 2:58 am

Re: Sorting,Paging and Search not working in Custom Folder

Post by waltersnowslin » Wed Oct 14, 2009 5:26 am

Code: Select all

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CommerceBuilder.Common;
using CommerceBuilder.Catalog;
using CommerceBuilder.Products;
using CommerceBuilder.Utility;

public partial class ConLib_Custom_StridaCategoryGridPage : System.Web.UI.UserControl
{
    private int _Cols = 3;
    private int _Rows = 5;
    private int _PageSize;
    private int _ManufacturerId = 0;
    private string _Keywords = string.Empty;
    private Category _Category;
    private int _HiddenPageIndex;
    private int _SearchResultCount;
    private int _LastPageIndex;
    ASP.conlib_categorysearchsidebar_ascx searchBar = null;

    public int CategoryId
    {
        get
        {
            if (ViewState["CategoryId"] == null)
                ViewState["CategoryId"] = PageHelper.GetCategoryId();
            return (int)ViewState["CategoryId"];
        }
        set
        {
            ViewState["CategoryId"] = value;
        }
    }

    private void BindPage()
    {
        //BIND THE DISPLAY ELEMENTS
        if (_Category != null)
        {
            Page.Title = _Category.Name;
            CategoryBreadCrumbs1.CategoryId = this.CategoryId;
            Caption.Text = _Category.Name;
            BindSubCategories();
        }
        else
        {
            CategoryHeaderPanel.Visible = false;
        }
        BindSearchResultsPanel();        
    }

    private bool _PagingVarsInitialized = false;
    private void InitializePagingVars(bool forceRefresh)
    {
        Trace.Write("Initialize Paging Vars");
        if (!_PagingVarsInitialized || forceRefresh)
        {
            _HiddenPageIndex = AlwaysConvert.ToInt(HiddenPageIndex.Value);
            _SearchResultCount = ProductDataSource.NarrowSearchCount(_Keywords, this.CategoryId, _ManufacturerId, 0, 0);
            _LastPageIndex = ((int)Math.Ceiling(((double)_SearchResultCount / (double)_PageSize))) - 1;
            _PagingVarsInitialized = true;
        }
    }

    void ProcessSidebarEvent(object sender, EventArgs e)
    {
        ASP.conlib_categorysearchsidebar_ascx searchBar = (ASP.conlib_categorysearchsidebar_ascx)sender;
        this.CategoryId = searchBar.CategoryId;
        _Category = CategoryDataSource.Load(this.CategoryId);
        _ManufacturerId = searchBar.ManufacturerId;
        _Keywords = searchBar.Keyword;
        HiddenPageIndex.Value = "0";
        InitializePagingVars(true);
    }

    protected void Page_Load(object sender, System.EventArgs e)
    {
        Trace.Write(this.GetType().ToString(), "Load Begin");
        _PageSize = (_Cols * _Rows);
        _Category = CategoryDataSource.Load(this.CategoryId);
        //EXIT PROCESSING IF CATEGORY IS INVALID OR MARKED PRIVATE
        if ((_Category == null) || (_Category.Visibility == CatalogVisibility.Private)) Response.Redirect(NavigationHelper.GetHomeUrl());
        if (!Page.IsPostBack)
        {
            //REGISTER THE PAGEVIEW
            CommerceBuilder.Services.AbleCommerceHttpModule.RegisterCatalogNode(this.CategoryId, CatalogNodeType.Category);
            //INITIALIZE SEARCH CRITERIA ON FIRST VISIT
            HiddenPageIndex.Value = Request.QueryString["p"];
            string tempSort = Request.QueryString["s"];
            if (!string.IsNullOrEmpty(tempSort))
            {
                ListItem item = SortResults.Items.FindByValue(tempSort);
                if (item != null) item.Selected = true;
            }
        }
        //LOOK FOR A SIDEBAR CONTROL TO LINK TO THIS SEARCH PAGE
        Trace.Write(this.GetType().ToString(), "Locating Sidebar");
        Control[] sidebars = PageHelper.FindControls(this.Page, typeof(ASP.conlib_categorysearchsidebar_ascx));
        if ((sidebars != null) && (sidebars.Length > 0))
        {
            searchBar = (ASP.conlib_categorysearchsidebar_ascx)sidebars[0];
            searchBar.UpdateResults += new EventHandler(ProcessSidebarEvent);
            //LOAD VALUES FROM 
            if (!Page.IsPostBack) searchBar.CategoryId = this.CategoryId;
            _Keywords = searchBar.Keyword;
            _ManufacturerId = searchBar.ManufacturerId;
        }
        SetPagerIndex();
        Trace.Write(this.GetType().ToString(), "Load Complete");

    }

    protected void BindSubCategories()
    {
        CategoryCollection allCategories = CategoryDataSource.LoadForParent(this.CategoryId, true);
        List<SubCategoryData> populatedCategories = new List<SubCategoryData>();
        foreach (Category category in allCategories)
        {
            int totalProducts = ProductDataSource.NarrowSearchCount(_Keywords, category.CategoryId, _ManufacturerId, 0, 0);
            if (totalProducts > 0)
            {
                populatedCategories.Add(new SubCategoryData(category.CategoryId, category.Name, category.NavigateUrl, totalProducts));
            }
        }
        if (populatedCategories.Count > 0)
        {
            SubCategoryPanel.Visible = true;
            SubCategoryRepeater.DataSource = populatedCategories;
            SubCategoryRepeater.DataBind();
        }
        else SubCategoryPanel.Visible = false;
    }

    public class SubCategoryData
    {
        private int _CategoryId;
        private string _Name;
        private int _ProductCount;
        private string _NavigateUrl;
        public int CategoryId { get { return _CategoryId; } }
        public string Name { get { return _Name; } }
        public int ProductCount { get { return _ProductCount; } }
        public string NavigateUrl { get { return _NavigateUrl; } }
        public SubCategoryData(int categoryId, string name, string navigateUrl, int productCount)
        {
            _CategoryId = categoryId;
            _Name = name;
            _NavigateUrl = navigateUrl;
            _ProductCount = productCount;
        }
    }

    private void BindSearchResultsPanel()
    {
        Trace.Write(this.GetType().ToString(), "Begin Bind Search Results");
        //INITIALIZE PAGING VARIABLES
        InitializePagingVars(false);
        //BIND THE RESULT HEADER
        BindResultHeader();
        //BIND THE PRODUCT LIST
        BindProductList();
        //BIND THE PAGING CONTROLS FOOTER
        BindPagingControls();
        //UPDATE AJAX PANEL
        SearchResultsAjaxPanel.Update();
        Trace.Write(this.GetType().ToString(), "End Bind Search Results");
    }

    protected void BindResultHeader()
    {
        //UPDATE THE RESULT INDEX MESSAGE
        int startRowIndex = (_PageSize * _HiddenPageIndex);
        int endRowIndex = startRowIndex + _PageSize;
        if (endRowIndex > _SearchResultCount) endRowIndex = _SearchResultCount;
        if (_SearchResultCount == 0) startRowIndex = -1;
        ResultIndexMessage.Text = string.Format(ResultIndexMessage.Text, (startRowIndex + 1), endRowIndex, _SearchResultCount);
    }

    protected void SortResults_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindPage();
    }

    protected void BindProductList()
    {
        ProductList.DataSource = ProductDataSource.NarrowSearch(_Keywords, this.CategoryId, _ManufacturerId, 0, 0, _PageSize, (_HiddenPageIndex * _PageSize), SortResults.SelectedValue);
        ProductList.DataBind();
        NoSearchResults.Visible = (_SearchResultCount == 0);
        SearchResultsAjaxPanel.Update();
    }

    protected void ProductList_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            //GENERATE TEMPLATE WITH HTML CONTROLS
            //TO OPTIMIZE OUTPUT SIZE
            PlaceHolder itemTemplate1 = (PlaceHolder)e.Item.FindControl("phItemTemplate1");
            PlaceHolder itemTemplate2 = (PlaceHolder)e.Item.FindControl("phItemTemplate2");
            if ((itemTemplate1 != null) && (itemTemplate2 != null))
            {
                Product product = (Product)e.Item.DataItem;
                string productUrl = this.Page.ResolveClientUrl(product.NavigateUrl);

                //OUTPUT LINKED THUMNAIL
                if (!string.IsNullOrEmpty(product.ThumbnailUrl))
                {
                    string thumbnail = string.Format("<a href=\"{0}\"><img src=\"{1}\" alt=\"{2}\" border=\"0\" class=\"Thumbnail\" /></a><br />", productUrl, ResolveUrl(product.ThumbnailUrl), product.ThumbnailAltText);
                    itemTemplate1.Controls.Add(new LiteralControl(thumbnail));
                }

                //OUTPUT LINKED NAME
                itemTemplate1.Controls.Add(new LiteralControl(string.Format("<a href=\"{0}\" class=\"highlight\">{1}</a><br />", productUrl, product.Name)));

                //OUTPUT RETAIL PRICE IF AVAILABLE
                if (product.MSRP > 0 && !product.UseVariablePrice)
                {
                    string msrp = string.Format("<span class=\"msrp\">{0:ulc}</span> ", product.MSRP);
                    itemTemplate1.Controls.Add(new LiteralControl(msrp));
                }

                //OUTPUT MANUFACTURER
                if (product.Manufacturer != null)
                {
                    itemTemplate2.Controls.Add(new LiteralControl("<br /><a href=\"Search.aspx?m=" + product.Manufacturer.ManufacturerId + "\">" + product.Manufacturer.Name + "</a>"));
                }

                //OUTPUT RATING
                if (Token.Instance.Store.Settings.ProductReviewEnabled != CommerceBuilder.Users.UserAuthFilter.None)
                {
                    itemTemplate2.Controls.Add(new LiteralControl(string.Format("<br /><img src=\"{0}\" />", NavigationHelper.GetRatingImage(product.Rating))));
                }
            }
        }
        else if (e.Item.ItemType == ListItemType.Separator)
        {
            //CHECK IF WE ARE AT THE END OF THE ROW
            int tempIndex = (e.Item.ItemIndex + 1);
            if ((tempIndex % ProductList.RepeatColumns) == 0)
            {
                //END OF ROW DETECTED, HIDE SEPARATOR
                e.Item.Controls.Clear();
                e.Item.CssClass = string.Empty;
            }
        }
    }

    #region PagingControls

    protected void BindPagingControls()
    {
        if (_LastPageIndex > 0)
        {
            PagerPanel.Visible = true;
            List<PagerLinkData> pagerLinkData = new List<PagerLinkData>();
            float tempIndex = ((float)_HiddenPageIndex / 10) * 10;
            int currentPagerIndex = (int)tempIndex;

            int lastPagerIndex = currentPagerIndex + _PageSize;
            if (lastPagerIndex > _LastPageIndex) lastPagerIndex = _LastPageIndex;
            string baseUrl;
            if (_Category != null) baseUrl = this.Page.ResolveClientUrl(_Category.NavigateUrl) + "?";
            else baseUrl = NavigationHelper.GetStoreUrl(this.Page, "~/Search.aspx?");
            if (!string.IsNullOrEmpty(_Keywords)) baseUrl += "k=" + Server.UrlEncode(_Keywords) + "&";
            if (_ManufacturerId != 0) baseUrl += "m=" + _ManufacturerId.ToString() + "&";
            if (!String.IsNullOrEmpty(SortResults.SelectedValue)) baseUrl += "s=" + SortResults.SelectedValue + "&";

            baseUrl += "p=";
            string navigateUrl;
            if (currentPagerIndex > 0)
            {
                navigateUrl = baseUrl + (currentPagerIndex - 1).ToString();
                pagerLinkData.Add(new PagerLinkData("<", navigateUrl, (currentPagerIndex - 1), true));
            }
            while (currentPagerIndex <= lastPagerIndex)
            {
                string linkText = ((int)(currentPagerIndex + 1)).ToString();
                if (currentPagerIndex != _HiddenPageIndex)
                {
                    navigateUrl = baseUrl + currentPagerIndex.ToString();
                    pagerLinkData.Add(new PagerLinkData(linkText, navigateUrl, currentPagerIndex, (currentPagerIndex != _HiddenPageIndex)));
                }
                else
                {
                    navigateUrl = "#";
                    pagerLinkData.Add(new PagerLinkData(linkText, navigateUrl, currentPagerIndex, (currentPagerIndex != _HiddenPageIndex), "current"));
                }
                currentPagerIndex++;
            }
            if (lastPagerIndex < _LastPageIndex)
            {
                navigateUrl = baseUrl + (lastPagerIndex + 1).ToString();
                pagerLinkData.Add(new PagerLinkData(">", navigateUrl, lastPagerIndex + 1, true));
            }
            PagerControls.DataSource = pagerLinkData;
            PagerControls.DataBind();
        }
        else
        {
            PagerPanel.Visible = false;
        }
    }

    public class PagerLinkData
    {
        private string _Text;
        private int _PageIndex;
        private string _NavigateUrl;
        public int PageIndex { get { return _PageIndex; } }
        private bool _Enabled;
        public string Text { get { return _Text; } }
        public string NavigateUrl { get { return _NavigateUrl; } }
        public bool Enabled { get { return _Enabled; } }
        private string _tagClass;
        public string TagClass { get { return _tagClass; } set { _tagClass = value; } }
        public PagerLinkData(string text, string navigateUrl, int pageIndex, bool enabled)
        {
            _Text = text;
            _NavigateUrl = navigateUrl;
            _PageIndex = pageIndex;
            _Enabled = enabled;
        }

        public PagerLinkData(string text, string navigateUrl, int pageIndex, bool enabled, string tagClass)
        {
            _Text = text;
            _NavigateUrl = navigateUrl;
            _PageIndex = pageIndex;
            _Enabled = enabled;
            _tagClass = tagClass;
        }
    }

    protected void PagerControls_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "Page")
        {
            InitializePagingVars(false);
            _HiddenPageIndex = AlwaysConvert.ToInt((string)e.CommandArgument);
            if (_HiddenPageIndex < 0) _HiddenPageIndex = 0;
            if (_HiddenPageIndex > _LastPageIndex) _HiddenPageIndex = _LastPageIndex;
            HiddenPageIndex.Value = _HiddenPageIndex.ToString();
        }
    }
    protected void SetPagerIndex()
    {
        InitializePagingVars(false);
        _HiddenPageIndex = AlwaysConvert.ToInt(Request.QueryString["p"]);
        if (_HiddenPageIndex < 0) _HiddenPageIndex = 0;
        if (_HiddenPageIndex > _LastPageIndex) _HiddenPageIndex = _LastPageIndex;
        HiddenPageIndex.Value = _HiddenPageIndex.ToString();
    }
    #endregion

    protected void Page_PreRender(object sender, EventArgs e)
    {
        //BIND PAGE
        BindPage();
    }   
}


Post Reply