???????为什么?谁知道,我用的是编辑按钮一点生成两个按钮,可是在点更新就不执行COMMAND事件了,CANEL也是,为什么?
谁知道?郁闷一下午,MSDN上也没有说明?
<asp:DataGrid id="ItemsGrid" BorderColor="Black" BorderWidth="1px" CellPadding="3" OnEditCommand="ItemsGrid_Edit"
OnCancelCommand="ItemsGrid_Cancel" OnUpdateCommand="ItemsGrid_Update" AutoGenerateColumns="False"
runat="server">
<HeaderStyle BackColor="#AAAADD"></HeaderStyle>
<Columns>
<asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Update" ItemStyle-Wrap="false" HeaderText="Edit Controls"
ButtonType="PushButton" />
<asp:ButtonColumn Text="Delete" HeaderText="Delete item" CommandName="Delete"></asp:ButtonColumn>
<asp:BoundColumn DataField="ResourceName" ReadOnly="True" HeaderText="Item"></asp:BoundColumn>
<asp:BoundColumn DataField="Price" HeaderText="Quantity" DataFormatString="{0:c}"></asp:BoundColumn>
<asp:BoundColumn DataField="Amount" HeaderText="Price"></asp:BoundColumn>
</Columns>
</asp:DataGrid>CS:public class WebForm2 : System.Web.UI.Page
{
public int iUserID;
public int iResourceID;
public int iOwnerID;
public string strResourceID;
public string strOwnerID;
public int iAmount;
public int iQuantity;
protected System.Web.UI.WebControls.DataGrid ItemsGrid;
protected System.Web.UI.WebControls.TextBox qtyText;
// 在此处放置用户代码以初始化页面
void Page_Load(Object sender, EventArgs e) 
{
 
strOwnerID = Request.QueryString["OwnerID"];
strResourceID = Request.QueryString["ResourceID"];
DataTable dtCartDetail;
if(strOwnerID!=null && strResourceID!=null)
{
iOwnerID  = Convert.ToInt32(strOwnerID);
iResourceID  = Convert.ToInt32(strResourceID);
if(Session["Cart"]!=null)
{
dtCartDetail = (DataTable)Session["Cart"];
dtCartDetail = F1Cart.CartAdd(iOwnerID,iResourceID,dtCartDetail);
}
else
{
dtCartDetail = F1Cart.CartInitialize();
dtCartDetail = F1Cart.CartAdd(iOwnerID,iResourceID,dtCartDetail);
}
}
else  
{
if(Session["Cart"]!=null)
{
dtCartDetail =(DataTable)Session["Cart"];
}
else
{
dtCartDetail = F1Cart.CartInitialize();
}
}
Session["Cart"] = dtCartDetail;
if(!Page.IsPostBack)
{
ItemsGrid.DataSource =dtCartDetail.DefaultView;
ItemsGrid.DataBind();
}
dtCartDetail.Dispose();

// else
// {
// dtCartDetail = (DataTable)Session["Cart"];
// }

                   
}
 
public void ItemsGrid_Edit(Object sender, DataGridCommandEventArgs e) 
{ // Set the EditItemIndex property to the index of the item clicked
// in the DataGrid control to enable editing for that item. Be sure
// to rebind the DateGrid to the data source to refresh the control.
ItemsGrid.EditItemIndex = e.Item.ItemIndex;
ItemsGrid.DataBind(); }
 
public void ItemsGrid_Cancel(Object sender, DataGridCommandEventArgs e) 
{ // Set the EditItemIndex property to -1 to exit editing mode. 
// Be sure to rebind the DateGrid to the data source to refresh
// the control.
ItemsGrid.EditItemIndex = -1;
ItemsGrid.DataBind(); }
 
public void ItemsGrid_Update(Object sender, DataGridCommandEventArgs e) 
{ ItemsGrid.DataSource = F1Cart.CartUpdate(e.Item.ItemIndex,Convert.ToInt32(e.Item.Cells[2].Text.ToString()),(DataTable)Session["Cart"]);
ItemsGrid.EditItemIndex = -1;
ItemsGrid.DataBind(); }
void DeleteItem(DataGridCommandEventArgs e)
{ DataTable dtCartDetail = F1Cart.CartDelete(e.Item.ItemIndex,(DataTable)Session["Cart"]);
Session["Cart"]   = dtCartDetail;
ItemsGrid.DataSource = dtCartDetail.DefaultView;
ItemsGrid.DataBind();
dtCartDetail.Dispose(); }
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{    
this.Load += new System.EventHandler(this.Page_Load); }
#endregion }
}