Esempio n. 1
0
        protected void BindData()
        {
            try
            {
                if (!String.IsNullOrEmpty(txtSearchTerm.Text))
                {
                    //can be removed
                    if (String.IsNullOrEmpty(txtSearchTerm.Text))
                    {
                        throw new NopException(LocalizationManager.GetLocaleResourceString("Search.SearchTermCouldNotBeEmpty"));
                    }
                    if (txtSearchTerm.Text.Length < 3)
                    {
                        throw new NopException(LocalizationManager.GetLocaleResourceString("Search.SearchTermMinimumLengthIs3Characters"));
                    }

                    int totalRecords           = 0;
                    ProductCollection products = ProductManager.GetAllProducts(txtSearchTerm.Text,
                                                                               chSearchInProductDescriptions.Checked, 100, 0, out totalRecords);

                    lvProducts.DataSource = products;
                    lvProducts.DataBind();
                    lvProducts.Visible    = products.Count > 0;
                    pagerProducts.Visible = products.Count > pagerProducts.PageSize;
                    lblNoResults.Visible  = !lvProducts.Visible;

                    int customerID = 0;
                    if (NopContext.Current.User != null)
                    {
                        customerID = NopContext.Current.User.CustomerID;
                    }
                    SearchLogManager.InsertSearchLog(txtSearchTerm.Text, customerID, DateTime.Now);
                }
                else
                {
                    pagerProducts.Visible = false;
                    lvProducts.Visible    = false;
                }
            }
            catch (Exception exc)
            {
                lvProducts.Visible    = false;
                pagerProducts.Visible = false;
                lblError.Text         = Server.HtmlEncode(exc.Message);
            }
        }
Esempio n. 2
0
        protected void BindData()
        {
            try
            {
                string keywords = txtSearchTerm.Text.Trim();

                if (!String.IsNullOrEmpty(keywords))
                {
                    int searchTermMinimumLength = SettingManager.GetSettingValueInteger("Search.ProductSearchTermMinimumLength", 3);
                    if (keywords.Length < searchTermMinimumLength)
                    {
                        throw new NopException(string.Format(LocalizationManager.GetLocaleResourceString("Search.SearchTermMinimumLengthIsNCharacters"), searchTermMinimumLength));
                    }

                    bool    advSearch                   = cbAdvancedSearch.Checked;
                    int     categoryId                  = 0;
                    int     manufacturerId              = 0;
                    decimal?minPriceConverted           = null;
                    decimal?maxPriceConverted           = null;
                    bool    searchInProductDescriptions = false;
                    if (advSearch)
                    {
                        //categories
                        if (ddlCategories.Items.Count > 0)
                        {
                            categoryId = int.Parse(this.ddlCategories.SelectedItem.Value);
                        }

                        //manufacturers
                        if (ddlManufacturers.Items.Count > 0)
                        {
                            manufacturerId = int.Parse(this.ddlManufacturers.SelectedItem.Value);
                        }

                        //min price
                        decimal?minPrice = null;
                        try
                        {
                            if (!string.IsNullOrEmpty(txtPriceFrom.Text.Trim()))
                            {
                                minPrice = decimal.Parse(txtPriceFrom.Text.Trim());
                                if (minPrice.HasValue)
                                {
                                    minPriceConverted = CurrencyManager.ConvertCurrency(minPrice.Value, NopContext.Current.WorkingCurrency, CurrencyManager.PrimaryStoreCurrency);
                                }
                            }
                        }
                        catch
                        {
                            txtPriceFrom.Text = string.Empty;
                        }

                        //max price
                        decimal?maxPrice = null;
                        try
                        {
                            if (!string.IsNullOrEmpty(txtPriceTo.Text.Trim()))
                            {
                                maxPrice = decimal.Parse(txtPriceTo.Text.Trim());
                                if (maxPrice.HasValue)
                                {
                                    maxPriceConverted = CurrencyManager.ConvertCurrency(maxPrice.Value, NopContext.Current.WorkingCurrency, CurrencyManager.PrimaryStoreCurrency);
                                }
                            }
                        }
                        catch
                        {
                            txtPriceTo.Text = string.Empty;
                        }

                        //search in descriptions
                        searchInProductDescriptions = cbSearchInProductDescriptions.Checked;
                    }

                    int totalRecords = 0;
                    var products     = ProductManager.GetAllProducts(categoryId,
                                                                     manufacturerId, 0, null,
                                                                     minPriceConverted, maxPriceConverted,
                                                                     keywords, searchInProductDescriptions,
                                                                     100, 0, new List <int>(), out totalRecords);

                    lvProducts.DataSource = products;
                    lvProducts.DataBind();
                    lvProducts.Visible    = products.Count > 0;
                    pagerProducts.Visible = products.Count > pagerProducts.PageSize;
                    lblNoResults.Visible  = !lvProducts.Visible;

                    int customerId = 0;
                    if (NopContext.Current.User != null)
                    {
                        customerId = NopContext.Current.User.CustomerId;
                    }
                    SearchLogManager.InsertSearchLog(txtSearchTerm.Text, customerId, DateTime.UtcNow);
                }
                else
                {
                    pagerProducts.Visible = false;
                    lvProducts.Visible    = false;
                }
            }
            catch (Exception exc)
            {
                lvProducts.Visible    = false;
                pagerProducts.Visible = false;
                lblError.Text         = Server.HtmlEncode(exc.Message);
            }
        }