public void dgMailList_DeleteCommand(object source,System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string mailid = dgMailList.DataKeys[e.Item.ItemIndex].ToString();
string sqlCom = "delete FROM CM_Maillist where MailID="+mailid;
//定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["CM_admin"].ToString());
//定义命令对象
SqlCommand cmd = new SqlCommand(sqlCom,conn);
//打开数据连接
conn.Open();
try
{
//执行SQL命令
cmd.ExecuteNonQuery();
//取得当前页的索引
lastEditedPage = dgMailList.CurrentPageIndex;
//如果有多页并且当前页中的项数仅有一项
if ((dgMailList.PageCount - dgMailList.CurrentPageIndex) == 1 && dgMailList.Items.Count == 1)
{
if (dgMailList.PageCount > 1)
{
lastEditedPage = lastEditedPage - 1;
}
else
{
lastEditedPage = 0;
}
}
dgMailList.CurrentPageIndex = lastEditedPage;
DataGridDataBind();
}
catch(SqlException err)
{
//输出异常信息
Response.Write(err.ToString());
}
finally
{
//关闭连接对象
conn.Close();
}
}
public void dgMailList_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem||e.Item.ItemType==ListItemType.EditItem)
{
TableCell mytablecell;
mytablecell=e.Item.Cells[2];
LinkButton btndel=(LinkButton)mytablecell.Controls[0];
btndel.Attributes.Add("onclick","return confirm('你确定删除该邮件吗?');");
btndel.Text="删除";
}
}