谁能帮我解释一下foreach里的这几句代码的意思?不太理解e.CommandArgument
protectedvoidDataList1_ItemCommand(objectsource,DataListCommandEventArgse){if(e.Comma...
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "addToCart")
{
DataTable dt;
if (Session["gwc"] == null)
{
dt = new DataTable();
//int[] arr = new int[] { 1, 2, 3, 4 };
dt.Columns.AddRange(new DataColumn[] { new DataColumn("产品id"),new DataColumn("产品名称"),new DataColumn("单价"), new DataColumn("数量") });
}
else
{
dt = (DataTable)Session["gwc"];
}
bool flag = false;
foreach (DataRow dr in dt.Rows)
{
if (e.CommandArgument.ToString() == dr["产品id"].ToString())
{
dr["数量"] = int.Parse(dr["数量"].ToString()) + int.Parse(((DropDownList)e.Item.FindControl("listAmount")).SelectedValue);
flag = true;
break;
}
}
if (!flag)
{
DataRow dr = dt.NewRow();
dr["产品id"] = e.CommandArgument;
dr["产品名称"] = ((Label)e.Item.FindControl("Label1")).Text;
dr["单价"] = ((Label)e.Item.FindControl("Label5")).Text;
dr["数量"] = ((DropDownList)e.Item.FindControl("listAmount")).SelectedValue;
//dr["单价"] = DataBinder.Eval(e.Item.DataItem, "单价").ToString();
dt.Rows.Add(dr);
}
dt.AcceptChanges();
Session["gwc"] = dt;
Response.Redirect("showCart.aspx");
} 展开
{
if (e.CommandName == "addToCart")
{
DataTable dt;
if (Session["gwc"] == null)
{
dt = new DataTable();
//int[] arr = new int[] { 1, 2, 3, 4 };
dt.Columns.AddRange(new DataColumn[] { new DataColumn("产品id"),new DataColumn("产品名称"),new DataColumn("单价"), new DataColumn("数量") });
}
else
{
dt = (DataTable)Session["gwc"];
}
bool flag = false;
foreach (DataRow dr in dt.Rows)
{
if (e.CommandArgument.ToString() == dr["产品id"].ToString())
{
dr["数量"] = int.Parse(dr["数量"].ToString()) + int.Parse(((DropDownList)e.Item.FindControl("listAmount")).SelectedValue);
flag = true;
break;
}
}
if (!flag)
{
DataRow dr = dt.NewRow();
dr["产品id"] = e.CommandArgument;
dr["产品名称"] = ((Label)e.Item.FindControl("Label1")).Text;
dr["单价"] = ((Label)e.Item.FindControl("Label5")).Text;
dr["数量"] = ((DropDownList)e.Item.FindControl("listAmount")).SelectedValue;
//dr["单价"] = DataBinder.Eval(e.Item.DataItem, "单价").ToString();
dt.Rows.Add(dr);
}
dt.AcceptChanges();
Session["gwc"] = dt;
Response.Redirect("showCart.aspx");
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询