ProView.RowFilter="ProName='"+item+"'";
if(ProView.Count>0)
{
ProView.Delete(0);
}
ProView.RowFilter="";
this.DataList1.EditItemIndex=-1;
BindList();
重点在红色部分,,谢谢!!!
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace DataListTest3_25
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataList DataList1;
DataTable Pro=new DataTable();
DataView ProView;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
GetSource();
if(!IsPostBack)
{
BindList();
}
}
private void GetSource()
{
if(Session["ShoppingCart"]==null)
{
DataRow dr;
Pro.Columns.Add(new DataColumn("ProAmount",typeof(int)));
Pro.Columns.Add(new DataColumn("ProName",typeof(string)));
Pro.Columns.Add(new DataColumn("ProPrice",typeof(double)));
Session["ShoppingCart"]=Pro;
for(int i=1;i<=9;i++)
{
dr=Pro.NewRow();
if((i%2)!=0)
{
dr[0]=2;
}
else
{
dr[0]=1;
}
dr[1]="商品"+i.ToString();
dr[2]=(1.23*(i+1));
Pro.Rows.Add(dr);
}
}
else
{
Pro=(DataTable)Session["ShoppingCart"];
}
ProView=new DataView(Pro);
ProView.Sort="ProName";
return;
}
private void BindList()
{
this.DataList1.DataSource=ProView;
this.DataList1.DataBind();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DataList1.CancelCommand += new System.Web.UI.WebControls.DataListCommandEventHandler(this.DataList1_CancelCommand);
this.DataList1.EditCommand += new System.Web.UI.WebControls.DataListCommandEventHandler(this.DataList1_EditCommand);
this.DataList1.DeleteCommand += new System.Web.UI.WebControls.DataListCommandEventHandler(this.DataList1_DeleteCommand);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void DataList1_EditCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
{
this.DataList1.EditItemIndex=e.Item.ItemIndex;
BindList();
}
private void DataList1_CancelCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
{
this.DataList1.EditItemIndex=-1;
BindList();
}
private void DataList1_DeleteCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
{
string item=((Label)(e.Item.FindControl("ItemLabel"))).Text;
ProView.RowFilter="ProName='"+item+"'";
if(ProView.Count>0)
{
ProView.Delete(0);
}
ProView.RowFilter="";
this.DataList1.EditItemIndex=-1;
BindList();
}
}
}