protected void gvFilters_RowDataBound(object sender, GridViewRowEventArgs e)
 {
     if (e.Row.RowType == DataControlRowType.DataRow)
     {
         //Fill checked dropDownList
         CheckBoxList ddl      = (CheckBoxList)e.Row.FindControl("ddlSubFilter") as CheckBoxList;
         Int64        filterId = Int64.Parse(gvFilters.DataKeys[e.Row.RowIndex].Value.ToString());
         foreach (ListItem item in ddl.Items)
         {
             if (ProductFilter.FetchByID(ProductId, filterId, Convert.ToInt64(item.Value)) != null)
             {
                 item.Selected = true;
             }
         }
     }
 }
        private void FillDdlSubFilter(Int64 ProductId, Int64 FilterId, CheckBoxList ddlSubFilter)
        {
            ddlSubFilter.Items.Clear();
            List <FilterUI> filetrList = ProductController.GetAllFilter();

            foreach (var filter in filetrList)
            {
                if (filter.FilterId == FilterId)
                {
                    int index = 0;
                    foreach (var subFilter in filter.LstSubFilter)
                    {
                        ddlSubFilter.Items.Add(new ListItem(subFilter.SubFilterName, subFilter.SubFilterId.ToString()));
                        if (ProductFilter.FetchByID(ProductId, FilterId, subFilter.SubFilterId) != null)
                        {
                            ddlSubFilter.Items[index].Selected = true;
                        }
                        index++;
                    }
                }
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!Page.IsValid)
            {
                return;
            }

            Product product = null;

            if (IsNewMode)
            {
                product = new Product();
                product.SendSupplier = cbxIsSendSupplier.Checked;
                product.IsDeleted    = false;
            }
            else
            {
                product = Product.FetchByID(ProductId);
            }
            Product p = Product.FetchByCode(txtProductCode.Text);

            if (p != null && p.ProductId != ProductId)
            {
                Master.MessageCenter.DisplayErrorMessage(ProductsStrings.GetText(@"ProductCodeAlreadyExists"));
                return;
            }
            if (txtProductNum.Text != "")
            {
                Product p1 = Product.FetchByProductNum(Convert.ToInt64(txtProductNum.Text));
                if (p1 != null && p1.ProductId != ProductId)
                {
                    Master.MessageCenter.DisplayErrorMessage(ProductsStrings.GetText(@"ProductNumAlreadyExists"));
                    return;
                }
                else
                {
                    product.ProductNum = Convert.ToInt64(txtProductNum.Text);
                }
            }
            else
            {
                product.ProductNum = null;
            }
            product.ProductName     = txtProductName.Text;
            product.ProductCode     = txtProductCode.Text;
            product.Amount          = txtProductAmount.Text;
            product.Description     = txtProductDescription.Text;
            product.CategoryId      = Convert.ToInt64(ddlCategory.SelectedValue);
            product.SubCategoryId   = Convert.ToInt64(ddlSubCategory.SelectedValue);
            product.RecomendedPrice = txtRecomendedPrice.Text.Trim() != "" ? Convert.ToDecimal(txtRecomendedPrice.Text.Trim()) : 0;
            if (fuImage.HasFile)
            {
                if (!IsNewMode)
                {
                    MediaUtility.DeleteImageFilePath("Product", product.ProductImage, 64, 64, 0);
                }
                string fn = MediaUtility.SaveFile(fuImage.PostedFile, "Product", 0);
                product.ProductImage = fn;
                imgImage.ImageUrl    = Snoopi.core.MediaUtility.GetImagePath("Product", product.ProductImage, 0, 64, 64);
                ImageFileHandler(fuImage, imgImage, btnDeleteImage, imgImage.ImageUrl);
            }
            else if (product.ProductImage != "" && fuImage.Visible)
            {
                MediaUtility.DeleteImageFilePath("Product", product.ProductImage, 64, 64, 0);
                product.ProductImage = "";
            }

            product.Save();
            ProductId = product.ProductId;
            int count = 0;

            foreach (ListItem item in ddlAnimalType.Items)
            {
                if (item.Selected)
                {
                    count++;
                    ProductAnimal productAnimal = ProductAnimal.FetchByID(ProductId, Convert.ToInt64(item.Value));
                    if (productAnimal == null)
                    {
                        productAnimal           = new ProductAnimal();
                        productAnimal.ProductId = ProductId;
                        productAnimal.AnimalId  = Convert.ToInt64(item.Value);
                        productAnimal.Save();
                    }
                }
                else
                {
                    ProductAnimal.Delete(ProductId, Convert.ToInt64(item.Value));
                }
            }
            int index = 0;

            //save filters
            foreach (GridViewRow row in gvFilters.Rows)
            {
                Int64        FilterId = Int64.Parse(gvFilters.DataKeys[index].Value.ToString());
                CheckBoxList lsbx     = (CheckBoxList)row.FindControl("ddlSubFilter");

                foreach (ListItem item in lsbx.Items)
                {
                    ProductFilter productFilter = ProductFilter.FetchByID(ProductId, FilterId, Convert.ToInt64(item.Value));
                    if (productFilter == null && item.Selected)
                    {
                        productFilter             = new ProductFilter();
                        productFilter.ProductId   = ProductId;
                        productFilter.FilterId    = FilterId;
                        productFilter.SubFilterId = Convert.ToInt64(item.Value);
                        productFilter.Save();
                    }
                    else if (productFilter != null && !item.Selected)
                    {
                        (new Query(ProductFilter.TableSchema).Where(ProductFilter.Columns.ProductId, productFilter.ProductId)
                         .AddWhere(ProductFilter.Columns.FilterId, productFilter.FilterId)
                         .AddWhere(ProductFilter.Columns.SubFilterId, productFilter.SubFilterId).Delete()).Execute();
                    }
                }
                index++;
            }

            if (IsNewMode)
            {
                if (cbxIsSendSupplier.Checked)
                {
                    EmailMessagingService.SendEmailNewProductToSupplier(product);
                }
                string successMessage = ProductsStrings.GetText(@"MessageProductCreated");
                string url            = @"EditProduct.aspx?ProductId=" + ProductId;
                url += @"&message-success=" + Server.UrlEncode(successMessage);
                Response.Redirect(url, true);
            }
            else
            {
                string successMessage = ProductsStrings.GetText(@"MessageProductSaved");
                Master.MessageCenter.DisplaySuccessMessage(successMessage);
            }
        }