X
XueWu
When I click the cancel button(EditCommandColumn) of the DataGrid control,
The DeleteCommand event is raised!!! Faint!!! Why this happend?? Why????????
Help help help help me, please!! Thanks!
The following code is my source code:
===========WebForm.aspx==================
<%@ Page Language="C#" AutoEventWireup="false" Debug="true" Trace="false"
Inherits="WebForm" Src="WebForm.aspx.cs" ContentType="text/html"
ResponseEncoding="gb2312" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Untitled Document</title>
<script language="javascript" type="text/javascript">
</script>
</head>
<body>
<Form Action="WebForm.aspx" Method="post" RunAt="server">
<aspataGrid
ID="myDG"
Width="100%"
BorderColor="#336699"
BorderWidth="1"
EnableViewState="false"
AutoGenerateColumns="false"
DataKeyField="CustomerID"
RunAt="server"<HeaderStyle BackColor="#333366" ForeColor="#CCCCCC"
HorizontalAlign="center" Wrap="false" />
<ItemStyle BackColor="#993300" ForeColor="#FFFFFF" Wrap="false" />
<Columns>
<asp:EditCommandColumn
ButtonType="PushButton"
EditText="Edit"
UpdateText="Update"
CancelText="Cancel"
/>
<asp:ButtonColumn ButtonType="PushButton" CommandName="Delete"
Text="Delete" />
<asp:BoundColumn DataField="CustomerID" HeaderText="CustomerID"
ReadOnly="true" />
<asp:BoundColumn DataField="CompanyName" HeaderText="CompanyName" />
<asp:BoundColumn DataField="ContactName" HeaderText="ContactName" />
<asp:BoundColumn DataField="ContactTitle" HeaderText="ContactTitle" />
<asp:BoundColumn DataField="Address" HeaderText="Address" />
</Columns>
</aspataGrid><br>
<asp:Button ID="firstPage" Text="First" Enabled="false" CommandName="First"
RunAt="server" />
<asp:Button ID="prePage" Text="Previous" Enabled="false"
CommandName="Previous" RunAt="server" />
<asp:Button ID="nextPage" Text="Next" Enabled="false" CommandName="Next"
RunAt="server" />
<asp:Button ID="lastPage" Text="Last" Enabled="false" CommandName="Last"
RunAt="server" />
</Form>
<br>
<asp:Label ID="msgLbl" Text="" ForeColor="#FF0000" RunAt="server" />
</body>
</html>
=================WebForm.aspx.cs======================
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
public class WebForm : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button firstPage;
protected System.Web.UI.WebControls.Button prePage;
protected System.Web.UI.WebControls.Button nextPage;
protected System.Web.UI.WebControls.Button lastPage;
protected System.Web.UI.WebControls.Label msgLbl;
protected System.Web.UI.WebControls.DataGrid myDG;
protected System.Data.SqlClient.SqlConnection sqlConn;
protected System.Data.SqlClient.SqlCommand selectCmd;
protected System.Data.SqlClient.SqlDataAdapter sqlDA;
protected System.Data.DataSet myDS;
private System.Int32 _pageSize;
private System.Int32 _currentPage;
private System.Int32 _totalPages;
private System.Int32 _totalRecords;
private void Page_Load(Object sender, EventArgs e)
{
selectCmd.CommandText = "select count(CustomerID) from Customers";
sqlConn.Open();
_totalRecords =
(System.Int32)Math.Ceiling(Convert.ToDouble(selectCmd.ExecuteScalar()));
sqlConn.Close();
_totalPages = (System.Int32)Math.Ceiling((double)_totalRecords /
_pageSize);
ViewState["TotalRecords"] = _totalRecords;
ViewState["TotalPages"] = _totalPages;
if(IsPostBack == false && _totalPages > 0)
GetPagingData(null, new CommandEventArgs("First", null));
else if(IsPostBack == true)
BindGrid();
}
private void GetPagingData(Object sender, CommandEventArgs e)
{
switch(e.CommandName)
{
case "First":
_currentPage = 1;
selectCmd.CommandText = "select top " + _pageSize + " * from Customers "
+
"order by CustomerID";
break;
case "Previous":
_currentPage = ((System.Int32)ViewState["CurrentPage"]) - 1;
selectCmd.CommandText = "select top " + _pageSize + " * from Customers
where " +
"CustomerID < @CustomerID_First order by CustomerID desc";
selectCmd.Parameters["@CustomerID_First"].Value =
(String)ViewState["CustomerID_First"];
break;
case "Next":
_currentPage = ((System.Int32)ViewState["CurrentPage"]) + 1;
selectCmd.CommandText = "select top " + _pageSize + " * from Customers
where " +
"CustomerID > @CustomerID_Last order by CustomerID";
selectCmd.Parameters["@CustomerID_Last"].Value =
(String)ViewState["CustomerID_Last"];
break;
case "Last":
_currentPage = (System.Int32)ViewState["TotalPages"];
_pageSize = (int)ViewState["TotalRecords"] %
(int)ViewState["TotalPages"];
selectCmd.CommandText = "select top " + _pageSize + " * from Customers "
+
"order by CustomerID desc";
break;
}
if(IsPostBack == true)
myDS.Tables["Customers"].Rows.Clear();
sqlDA.Fill(myDS, "Customers");
if(e.CommandName == "Last" || e.CommandName == "Previous")
myDS.Tables["Customers"].DefaultView.Sort = "CustomerID";
myDG.DataSource = myDS.Tables["Customers"].DefaultView;
myDG.DataBind();
ViewState["CurrentPage"] = _currentPage;
ViewState["CustomerID_First"] = myDG.Items[0].Cells[2].Text;
ViewState["CustomerID_Last"] = myDG.Items[myDG.Items.Count -
1].Cells[2].Text;
_totalPages = (System.Int32)ViewState["TotalPages"];
if(_currentPage > 1 && _currentPage < _totalPages)
{
firstPage.Enabled = true;
prePage.Enabled = true;
nextPage.Enabled = true;
lastPage.Enabled = true;
}
else if(_currentPage == 1)
{
firstPage.Enabled = false;
prePage.Enabled = false;
nextPage.Enabled = true;
lastPage.Enabled = true;
}
else if(_currentPage == _totalPages)
{
firstPage.Enabled = true;
prePage.Enabled = true;
nextPage.Enabled = false;
lastPage.Enabled = false;
}
}
private void MyDG_Edit(Object sender, DataGridCommandEventArgs e)
{
myDG.EditItemIndex = e.Item.ItemIndex;
myDS.Tables["Customers"].Rows.Clear();
BindGrid();
}
private void MyDG_Update(Object sender, DataGridCommandEventArgs e)
{
}
private void MyDG_CancelEdit(Object sender, DataGridCommandEventArgs e)
{
myDG.EditItemIndex = -1;
myDS.Tables["Customers"].Rows.Clear();
msgLbl.Text = "CancelEdit.";
BindGrid();
}
private void MyDG_Delete(Object sender, DataGridCommandEventArgs e)
{
myDG.EditItemIndex = -1;
msgLbl.Text = "Delete.";
}
private void BindGrid()
{
selectCmd.CommandText = "select top " + _pageSize + " * from Customers " +
"where CustomerID >= @CustomerID_First";
selectCmd.Parameters["@CustomerID_First"].Value =
(String)ViewState["CustomerID_First"];
sqlDA.Fill(myDS, "Customers");
myDG.DataSource = myDS.Tables["Customers"].DefaultView;
myDG.DataBind();
}
override protected void OnInit(EventArgs e)
{
InitializeComponents();
base.OnInit(e);
}
private void InitializeComponents()
{
sqlConn = new SqlConnection("Server=(local);Database=Northwind;Integrated
Security=SSPI;" +
"Persist Security Info=false");
selectCmd = new SqlCommand();
selectCmd.Connection = sqlConn;
selectCmd.Parameters.Add("@customerID_First", SqlDbType.NChar, 5);
selectCmd.Parameters["@customerID_First"].Value = "";
selectCmd.Parameters.Add("@customerID_Last", SqlDbType.NChar, 5);
selectCmd.Parameters["@customerID_Last"].Value = "";
sqlDA = new SqlDataAdapter(selectCmd);
myDS = new DataSet();
_pageSize = 10;
_currentPage = 0;
_totalPages = 0;
_totalRecords = 0;
firstPage.Command += new CommandEventHandler(GetPagingData);
prePage.Command += new CommandEventHandler(GetPagingData);
nextPage.Command += new CommandEventHandler(GetPagingData);
lastPage.Command += new CommandEventHandler(GetPagingData);
myDG.EditCommand += new DataGridCommandEventHandler(MyDG_Edit);
myDG.UpdateCommand += new DataGridCommandEventHandler(MyDG_Update);
myDG.CancelCommand += new DataGridCommandEventHandler(MyDG_CancelEdit);
myDG.DeleteCommand += new DataGridCommandEventHandler(MyDG_Delete);
Load += new EventHandler(Page_Load);
}
}
The DeleteCommand event is raised!!! Faint!!! Why this happend?? Why????????
Help help help help me, please!! Thanks!
The following code is my source code:
===========WebForm.aspx==================
<%@ Page Language="C#" AutoEventWireup="false" Debug="true" Trace="false"
Inherits="WebForm" Src="WebForm.aspx.cs" ContentType="text/html"
ResponseEncoding="gb2312" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Untitled Document</title>
<script language="javascript" type="text/javascript">
</script>
</head>
<body>
<Form Action="WebForm.aspx" Method="post" RunAt="server">
<aspataGrid
ID="myDG"
Width="100%"
BorderColor="#336699"
BorderWidth="1"
EnableViewState="false"
AutoGenerateColumns="false"
DataKeyField="CustomerID"
RunAt="server"<HeaderStyle BackColor="#333366" ForeColor="#CCCCCC"
HorizontalAlign="center" Wrap="false" />
<ItemStyle BackColor="#993300" ForeColor="#FFFFFF" Wrap="false" />
<Columns>
<asp:EditCommandColumn
ButtonType="PushButton"
EditText="Edit"
UpdateText="Update"
CancelText="Cancel"
/>
<asp:ButtonColumn ButtonType="PushButton" CommandName="Delete"
Text="Delete" />
<asp:BoundColumn DataField="CustomerID" HeaderText="CustomerID"
ReadOnly="true" />
<asp:BoundColumn DataField="CompanyName" HeaderText="CompanyName" />
<asp:BoundColumn DataField="ContactName" HeaderText="ContactName" />
<asp:BoundColumn DataField="ContactTitle" HeaderText="ContactTitle" />
<asp:BoundColumn DataField="Address" HeaderText="Address" />
</Columns>
</aspataGrid><br>
<asp:Button ID="firstPage" Text="First" Enabled="false" CommandName="First"
RunAt="server" />
<asp:Button ID="prePage" Text="Previous" Enabled="false"
CommandName="Previous" RunAt="server" />
<asp:Button ID="nextPage" Text="Next" Enabled="false" CommandName="Next"
RunAt="server" />
<asp:Button ID="lastPage" Text="Last" Enabled="false" CommandName="Last"
RunAt="server" />
</Form>
<br>
<asp:Label ID="msgLbl" Text="" ForeColor="#FF0000" RunAt="server" />
</body>
</html>
=================WebForm.aspx.cs======================
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
public class WebForm : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button firstPage;
protected System.Web.UI.WebControls.Button prePage;
protected System.Web.UI.WebControls.Button nextPage;
protected System.Web.UI.WebControls.Button lastPage;
protected System.Web.UI.WebControls.Label msgLbl;
protected System.Web.UI.WebControls.DataGrid myDG;
protected System.Data.SqlClient.SqlConnection sqlConn;
protected System.Data.SqlClient.SqlCommand selectCmd;
protected System.Data.SqlClient.SqlDataAdapter sqlDA;
protected System.Data.DataSet myDS;
private System.Int32 _pageSize;
private System.Int32 _currentPage;
private System.Int32 _totalPages;
private System.Int32 _totalRecords;
private void Page_Load(Object sender, EventArgs e)
{
selectCmd.CommandText = "select count(CustomerID) from Customers";
sqlConn.Open();
_totalRecords =
(System.Int32)Math.Ceiling(Convert.ToDouble(selectCmd.ExecuteScalar()));
sqlConn.Close();
_totalPages = (System.Int32)Math.Ceiling((double)_totalRecords /
_pageSize);
ViewState["TotalRecords"] = _totalRecords;
ViewState["TotalPages"] = _totalPages;
if(IsPostBack == false && _totalPages > 0)
GetPagingData(null, new CommandEventArgs("First", null));
else if(IsPostBack == true)
BindGrid();
}
private void GetPagingData(Object sender, CommandEventArgs e)
{
switch(e.CommandName)
{
case "First":
_currentPage = 1;
selectCmd.CommandText = "select top " + _pageSize + " * from Customers "
+
"order by CustomerID";
break;
case "Previous":
_currentPage = ((System.Int32)ViewState["CurrentPage"]) - 1;
selectCmd.CommandText = "select top " + _pageSize + " * from Customers
where " +
"CustomerID < @CustomerID_First order by CustomerID desc";
selectCmd.Parameters["@CustomerID_First"].Value =
(String)ViewState["CustomerID_First"];
break;
case "Next":
_currentPage = ((System.Int32)ViewState["CurrentPage"]) + 1;
selectCmd.CommandText = "select top " + _pageSize + " * from Customers
where " +
"CustomerID > @CustomerID_Last order by CustomerID";
selectCmd.Parameters["@CustomerID_Last"].Value =
(String)ViewState["CustomerID_Last"];
break;
case "Last":
_currentPage = (System.Int32)ViewState["TotalPages"];
_pageSize = (int)ViewState["TotalRecords"] %
(int)ViewState["TotalPages"];
selectCmd.CommandText = "select top " + _pageSize + " * from Customers "
+
"order by CustomerID desc";
break;
}
if(IsPostBack == true)
myDS.Tables["Customers"].Rows.Clear();
sqlDA.Fill(myDS, "Customers");
if(e.CommandName == "Last" || e.CommandName == "Previous")
myDS.Tables["Customers"].DefaultView.Sort = "CustomerID";
myDG.DataSource = myDS.Tables["Customers"].DefaultView;
myDG.DataBind();
ViewState["CurrentPage"] = _currentPage;
ViewState["CustomerID_First"] = myDG.Items[0].Cells[2].Text;
ViewState["CustomerID_Last"] = myDG.Items[myDG.Items.Count -
1].Cells[2].Text;
_totalPages = (System.Int32)ViewState["TotalPages"];
if(_currentPage > 1 && _currentPage < _totalPages)
{
firstPage.Enabled = true;
prePage.Enabled = true;
nextPage.Enabled = true;
lastPage.Enabled = true;
}
else if(_currentPage == 1)
{
firstPage.Enabled = false;
prePage.Enabled = false;
nextPage.Enabled = true;
lastPage.Enabled = true;
}
else if(_currentPage == _totalPages)
{
firstPage.Enabled = true;
prePage.Enabled = true;
nextPage.Enabled = false;
lastPage.Enabled = false;
}
}
private void MyDG_Edit(Object sender, DataGridCommandEventArgs e)
{
myDG.EditItemIndex = e.Item.ItemIndex;
myDS.Tables["Customers"].Rows.Clear();
BindGrid();
}
private void MyDG_Update(Object sender, DataGridCommandEventArgs e)
{
}
private void MyDG_CancelEdit(Object sender, DataGridCommandEventArgs e)
{
myDG.EditItemIndex = -1;
myDS.Tables["Customers"].Rows.Clear();
msgLbl.Text = "CancelEdit.";
BindGrid();
}
private void MyDG_Delete(Object sender, DataGridCommandEventArgs e)
{
myDG.EditItemIndex = -1;
msgLbl.Text = "Delete.";
}
private void BindGrid()
{
selectCmd.CommandText = "select top " + _pageSize + " * from Customers " +
"where CustomerID >= @CustomerID_First";
selectCmd.Parameters["@CustomerID_First"].Value =
(String)ViewState["CustomerID_First"];
sqlDA.Fill(myDS, "Customers");
myDG.DataSource = myDS.Tables["Customers"].DefaultView;
myDG.DataBind();
}
override protected void OnInit(EventArgs e)
{
InitializeComponents();
base.OnInit(e);
}
private void InitializeComponents()
{
sqlConn = new SqlConnection("Server=(local);Database=Northwind;Integrated
Security=SSPI;" +
"Persist Security Info=false");
selectCmd = new SqlCommand();
selectCmd.Connection = sqlConn;
selectCmd.Parameters.Add("@customerID_First", SqlDbType.NChar, 5);
selectCmd.Parameters["@customerID_First"].Value = "";
selectCmd.Parameters.Add("@customerID_Last", SqlDbType.NChar, 5);
selectCmd.Parameters["@customerID_Last"].Value = "";
sqlDA = new SqlDataAdapter(selectCmd);
myDS = new DataSet();
_pageSize = 10;
_currentPage = 0;
_totalPages = 0;
_totalRecords = 0;
firstPage.Command += new CommandEventHandler(GetPagingData);
prePage.Command += new CommandEventHandler(GetPagingData);
nextPage.Command += new CommandEventHandler(GetPagingData);
lastPage.Command += new CommandEventHandler(GetPagingData);
myDG.EditCommand += new DataGridCommandEventHandler(MyDG_Edit);
myDG.UpdateCommand += new DataGridCommandEventHandler(MyDG_Update);
myDG.CancelCommand += new DataGridCommandEventHandler(MyDG_CancelEdit);
myDG.DeleteCommand += new DataGridCommandEventHandler(MyDG_Delete);
Load += new EventHandler(Page_Load);
}
}