📄 newproduct.ascx.cs
字号:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using VinciDataAccess.DataAccess;
using VinciDataAccess.Entity;
public partial class UserControl_NewProduct : System.Web.UI.UserControl
{
//声明一个泛型接口
IList<ProductInfo> Iprod;
//声明ProductInfo类
ProductInfo prod;
//加入商品标记
private int i = 0;
#region //页面加载事件
protected void Page_Load(object sender, EventArgs e)
{
if (Application["ProductList"] == null)
{
Iprod = new List<ProductInfo>();
}
else
{
Iprod = (List<ProductInfo>)Application["ProductList"];
}
//页面第一次执行时
if (!IsPostBack)
{
//显示商品类别
BindCatagoris();
BindProduct(Convert.ToInt32(drpPrdCategory.SelectedItem.Value));
}
//else
//{
//}
}
#endregion
#region //添加商品事件
protected void btnAdd_Click(object sender, EventArgs e)
{
if (this.txtCount.Text != "")
{
if (IsInteger(this.txtCount.Text) == false)
{
Response.Write("<script>alert('sorry,产品数量输入错误!')</script>");
return;
}
else
{
// 第一次加入商品时
if (i == 0)
{
prod = new ProductInfo();
++i;
//显示商品信息
prod.strCategordName = this.drpPrdCategory.SelectedItem.ToString();
prod.strProdName = this.drpPrdName.SelectedItem.ToString();
prod.strProdCount = this.txtCount.Text;
prod.strDiscount = "0.9";
prod.strUnitPrice = Convert.ToString(GetPrdPrice(Convert.ToInt32(drpPrdName.SelectedItem.Value)));
Iprod.Add(prod);
//prod = (ProductInfo)Iprod[0];
Application["ProductList"] = Iprod;
//显示表头
ShowProductHead();
// 显示商品列表
ShowPrdList();
}
else
{
//显示商品信息
prod.strCategordName = this.drpPrdCategory.SelectedItem.Value;
prod.strProdName = this.drpPrdName.SelectedItem.Value;
prod.strProdCount = this.txtCount.Text;
prod.strUnitPrice = Convert.ToString(GetPrdPrice(int.Parse(this.drpPrdName.SelectedItem.Value)));
Iprod.Add(prod);
Application["ProductList"] = Iprod;
// 显示商品列表
ShowPrdList();
// 显示商品列表
//ShowPrdList(Iprod);
}
}
}
else
{
Response.Write("<script>alert('sorry,产品数量不能为空!')</script>");
}
}
#endregion
#region 给OrderDetailsEntity类属性赋值
public void SetOrderDetailsEntity(OrderDetailsEntity OrderDetails, int OrdID, int PrtID, decimal Uniprice, int Counts, string disCounts)
{
OrderDetails.OrderID = OrdID;
OrderDetails.ProductID = PrtID;
OrderDetails.UnitPrice = Uniprice ;
OrderDetails.Quantity = Counts;
OrderDetails.Discount =Convert.ToDouble( disCounts);
}
#endregion
#region/显示商品列表字段方法
public void ShowProductHead()
{
TableRow trOne = new TableRow();
TableCell tdPrtCatpry = new TableCell();
tdPrtCatpry.Text = "商品类别";
TableCell tdPrtName = new TableCell();
tdPrtName.Text = "商品名称";
TableCell tdPrtCount = new TableCell();
tdPrtCount.Text = "商品数量";
TableCell tdPrtUnitPrice = new TableCell();
tdPrtUnitPrice.Text = "商品单价";
TableCell tdPrtDisCount = new TableCell();
tdPrtDisCount.Text = "折扣";
TableCell tdPrtTotal = new TableCell();
tdPrtTotal.Text = "合计";
//TableCell tdPrtRstTotal = new TableCell();
//tdPrtRstTotal.Text = "总金额";
trOne.Cells.Add(tdPrtCatpry);
trOne.Cells.Add(tdPrtName);
trOne.Cells.Add(tdPrtCount);
trOne.Cells.Add(tdPrtUnitPrice);
trOne.Cells.Add(tdPrtDisCount);
trOne.Cells.Add(tdPrtTotal);
//trOne.Cells.Add(tdPrtRstTotal);
this.tblProductList.Rows.Add(trOne);
}
#endregion
#region //选择加入的商品信息类
public class ProductInfo
{
private string _strCategordName;
private string _strProdName;
private string _strProdCount;
private string _strUnitPrice;
private string _strDiscount;
private string _strPriceTotal;
private double _douResTotal;
public string strCategordName
{
get { return _strCategordName; }
set { _strCategordName = value; }
}
public string strProdName
{
get { return _strProdName; }
set { _strProdName = value; }
}
public string strProdCount
{
get { return _strProdCount; }
set { _strProdCount = value; }
}
public string strUnitPrice
{
get { return _strUnitPrice; }
set { _strUnitPrice = value; }
}
public string strDiscount
{
get { return _strDiscount; }
set { _strDiscount = value; }
}
public string strPriceTotal
{
get { return _strPriceTotal; }
set { _strPriceTotal = value; }
}
public double douResTotal
{
get { return _douResTotal; }
set { _douResTotal = value; }
}
}
#endregion
#region //显示数据库中Categories表类别字段方法
public void BindCatagoris()
{
CategoriesStore categtories = new CategoriesStore();
//绑定数据源
VinciDataAccess.Entity.CategoriesCollection cgrCotin = categtories.GetCategoriesList();
this.drpPrdCategory.DataSource = cgrCotin;
this.drpPrdCategory.DataValueField ="CategoryID";
this.drpPrdCategory.DataTextField = "CategoryName";
this.drpPrdCategory.DataBind();
}
#endregion
#region //查询类别商品事件
protected void drpPrdCategory_SelectedIndexChanged(object sender, EventArgs e)
{
BindProduct(Convert.ToInt32(drpPrdCategory.SelectedItem.Value));
if (i != 0)
{
//ShowPrdList();
}
}
#endregion
#region //显示数据库中Products产品名字段方法
public void BindProduct(int _CategoryID)
{
VinciDataAccess.DataAccess.ProductsStore products = new ProductsStore();
VinciDataAccess.Entity.ProductsCollection prdCollection = new ProductsCollection();
prdCollection = products.GetPrdListByCagryID(_CategoryID);
//绑定数据源
this.drpPrdName.DataSource = prdCollection;
this.drpPrdName.DataValueField = "ProductID";
this.drpPrdName.DataTextField = "ProductName";
this.drpPrdName.DataBind();
}
#endregion
#region // 根据产品ID返回该产品的单价的方法
public double GetPrdPrice(int PrdID)
{
double _dprice = 0;
VinciDataAccess.DataAccess.ProductsStore products = new ProductsStore();
VinciDataAccess.Entity.ProductsEntity prdInfo = new ProductsEntity();
prdInfo = products.GetProdcutInfo(PrdID);
_dprice = Convert.ToDouble(prdInfo.UnitPrice);
return _dprice;
}
#endregion
//在表中显示加入的商品信息
#region //显示商品列表
public void ShowPrdList()
{
//行标量
int Count = 0;
//Iprod = (IList<ProductInfo>)Application["ProductList"];
//商品金额总合计
double DoTotal = 0.00d;
//总商品金额合计
double DoRstTotal=0.00d;
//取出数据
foreach (ProductInfo pr in Iprod)
{
++Count;
//新建一行
TableRow tr = new TableRow();
TableCell tdCatpry = new TableCell();
tdCatpry.Text = pr.strCategordName;
TableCell tdName = new TableCell();
tdName.Text = pr.strProdName;
TableCell tdCount = new TableCell();
tdCount.Text = pr.strProdCount;
TableCell tdUintPrice = new TableCell();
tdUintPrice.Text = pr.strUnitPrice + "RMB";
TableCell tdDisCount = new TableCell();
tdDisCount.Text = pr.strDiscount;
TableCell tdTotal = new TableCell();
DoTotal = Convert.ToDouble(pr.strProdCount) * Convert.ToDouble(pr.strUnitPrice) * Convert.ToDouble(pr.strDiscount);
tdTotal.Text = DoTotal + "RMB";
//在总金额合计列
TableCell tbRstTotal = new TableCell();
DoRstTotal += DoTotal;
pr.douResTotal = DoRstTotal;
//在表中新建一个编辑列
//TableCell tbEdit = new TableCell();
//LinkButton LinkbtnEdit = new LinkButton();
//LinkbtnEdit.ID ="btnEdit" +this.tblProductList.Rows.Count.ToString();
//LinkbtnEdit.Text = "编辑";
//LinkbtnEdit.Click += new System.EventHandler(LinkbtnEdit_Click);
//tbEdit.Controls.Add(button);
////在表中新建一个删除列
TableCell tbDelete = new TableCell();
LinkButton LinkbtnDelete = new LinkButton();
LinkbtnDelete.ID = "btnDele";
LinkbtnDelete.PostBackUrl = "~/PO06.aspx?index=" + Count.ToString();
//LinkbtnDelete.Click += new System.EventHandler(LinkbtnDelete_Click);
LinkbtnDelete.Text = "删除";
tbDelete.Controls.Add(LinkbtnDelete);
////在表中新建一个更新列
//TableCell tbOK = new TableCell();
//LinkButton LinkbtnUpdate = new LinkButton();
//LinkbtnUpdate.ID = "btnUpdate" + this.tblProductList.Rows.Count.ToString();
//LinkbtnUpdate.Click += new System.EventHandler(LinkbtnUpdate_Click);
//LinkbtnUpdate.Text = "更新";
//tbOK.Controls.Add(LinkbtnUpdate);
tr.Cells.Add(tdCatpry);
tr.Cells.Add(tdName);
tr.Cells.Add(tdCount);
tr.Cells.Add(tdUintPrice);
tr.Cells.Add(tdDisCount);
tr.Cells.Add(tdTotal);
//tr.Cells.Add(tbEdit);
tr.Cells.Add(tbDelete);
//tr.Cells.Add(tbOK);
this.tblProductList.Rows.Add(tr);
}
this.lblRetotal.Text = "总金额:"+prod.douResTotal.ToString()+"RMB";
Application["ProductList"] = Iprod;
}
#endregion
#region //验证函数方法 是否输入为整数
public bool IsInteger( string _reg)
{
bool _isCount=false;
Regex reg = new Regex(@"^[0-9]*[1-9][0-9]*$");
if (reg.IsMatch(_reg))
{
_isCount=true;
return _isCount;
}
return _isCount;
}
#endregion
#region //动态添加的删除事件
protected void LinkbtnDelete_Click(object sender, System.EventArgs e)
{
Response.Write("<script>alert('test')</script>");
//取出表中点击的索引号
//string _ID = ((LinkButton)sender).ID.ToString();
//int _length = Convert.ToInt32(_ID.Length- 7);
//int _index = Convert.ToInt32(_ID.Substring(6, _length));
Response.Write("<script>alert('text')</script>");
//// 移除要删除的商品记录
//Iprod.Remove(Iprod[_index]);
//Session["ProductList"] = Iprod;
////显示商品表
//ShowPrdList();
}
#endregion
#region //动态添加的更新事件
private void LinkbtnUpdate_Click(object sender, System.EventArgs e)
{
//取出表中点击的索引号
string _ID = ((LinkButton)sender).ID.ToString();
int _length = Convert.ToInt32(_ID.Length- 7);
int _index = Convert.ToInt32(_ID.Substring(6, _length));
//将修改的值更新到Iprod中
Iprod[_index].strUnitPrice = this.tblProductList.Rows[_index].Cells[3].Text;
//保存在Session["ProductList"]中
Session["ProductList"] = Iprod;
//显示更新后的商品列表
ShowPrdList();
}
#endregion
#region //动态添加的编辑事件
private void LinkbtnEdit_Click(object sender, System.EventArgs e)
{
//取出表中点击的索引号
string _ID = ((LinkButton)sender).ID.ToString();
int _length = Convert.ToInt32(_ID.Length - 7);
int _index = Convert.ToInt32(_ID.Substring(8, _length));
//编辑价格
TextBox tbUnprice = new TextBox();
tbUnprice.ID="tb";
tbUnprice.Text = this.tblProductList.Rows[_index].Cells[3].Text;
this.tblProductList.Rows[_index].Cells[3].Controls.Add(tbUnprice);
}
#endregion
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -