コード例 #1
0
        protected void ButtonEkle_Click(object sender, EventArgs e)
        {
            if (ButtonEkle.Text.ToUpper() == "EKLE")
            {
                int     sonID   = 0;
                Product sonUrun = repoUrun.GetAll().OrderByDescending(o => o.ID).FirstOrDefault();
                if (sonUrun != null)
                {
                    sonID = sonUrun.ID;
                }
                sonID++;
                string dosya = "/contents/" + MyTool.DosyaYoluOlustur(resim.Value, sonID.ToString());

                Product yeniUrun = new Product
                {
                    Name        = Name.Text,
                    Description = Description.Text,
                    Stock       = Convert.ToInt32(stock.Text),
                    Price       = Convert.ToDecimal(Price.Text),
                    BrandID     = Convert.ToInt32(DropDownBrand.SelectedValue),
                    Picture     = dosya
                };
                repoUrun.Add(yeniUrun);
                resim.PostedFile.SaveAs(Server.MapPath(dosya));
                foreach (ListItem item in CheckBoxCategori.Items)
                {
                    if (item.Selected)
                    {
                        int cateID = Convert.ToInt32(item.Value);
                        repoProductCategory.Add(new ProductCategory {
                            ProductID = yeniUrun.ID, CategoryID = cateID
                        });
                    }
                }
            }
            else//yani güncelleme işlemi ise
            {
                urunID = Convert.ToInt32(LabelID.Text);
                Product urunGuncelle = repoUrun.GetBy(g => g.ID == urunID);
                if (urunGuncelle != null)
                {
                    urunGuncelle.Name        = Name.Text;
                    urunGuncelle.Description = Description.Text;
                    urunGuncelle.Stock       = Convert.ToInt32(stock.Text);
                    urunGuncelle.Price       = Convert.ToDecimal(Price.Text);
                    urunGuncelle.BrandID     = Convert.ToInt32(DropDownBrand.SelectedValue);
                    if (resim.Value != "")
                    {
                        string dosya = "/contents/" + MyTool.DosyaYoluOlustur(resim.Value, urunGuncelle.ID.ToString());
                        resim.PostedFile.SaveAs(Server.MapPath(dosya));
                        urunGuncelle.Picture = dosya;
                    }
                    repoUrun.Update(urunGuncelle);

                    //önce ürünün kategorilerini sil
                    List <ProductCategory> productCategories = repoProductCategory.GetAll().Where(w => w.ProductID == urunID).ToList();
                    repoProductCategory.RemoveRange(productCategories);
                    //ürün için seçili katregorileri ekle
                    foreach (ListItem item in CheckBoxCategori.Items)
                    {
                        if (item.Selected)
                        {
                            int cateID = Convert.ToInt32(item.Value);
                            repoProductCategory.Add(new ProductCategory {
                                ProductID = urunID, CategoryID = cateID
                            });
                        }
                    }
                    ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Güncellendi');", true);
                }
                ButtonEkle.Text = "EKLE";
            }
            //if (Request.QueryString["MarkaID"] != null)
            //{
            //    Response.Redirect("Urunler.aspx?MarkaID=" + Request.QueryString["MarkaID"] + "&MarkaAd=" + Request.QueryString["MarkaAd"]);
            //}
            //else
            //    Response.Redirect("Urunler.aspx");
            Response.Redirect("/admin/Default.aspx");
        }