如何在DataGrid删除时弹出一个Confirm对话框,提示"是否删除", 按"确定"删除,否则不处理....我现在无论是"确定"还是"取消".它都把我的数据删了..在这里贴上我的源码....各位帮帮手..看哪里有问题了,也可以把你们可运行的代码贴上来...谢谢...
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;
using System.Data.SqlClient;
namespace 删除确认
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid grdTest;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;
private DataSet ds = new DataSet();
private void Page_Load(object sender, System.EventArgs e)
{
if(!this.IsPostBack)
{
string strConnection = "Data Source=127.0.0.1;Initial Catalog=pubs;User Id=sa;Password=123;";
SqlConnection myConnection = new SqlConnection(strConnection);
SqlDataAdapter myAdapter = new SqlDataAdapter("SELECT * FROM bb",myConnection);
myAdapter.Fill(ds);
this.grdTest.DataSource = ds.Tables[0].DefaultView;
this.grdTest.DataKeyField = "ID";
this.grdTest.DataBind();
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.grdTest.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.grdTest_ItemCommand);
this.grdTest.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.grdTest_ItemDataBound);
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void grdTest_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
/*
switch(e.Item.ItemType)
{
case ListItemType.Item:
case ListItemType.AlternatingItem:
case ListItemType.EditItem:
{
Button btn = (Button)e.Item.FindControl("btnDelete");
btn.Attributes.Add("onclick", "javascript:confirm('你是否确定删除这条记录');");
break;
}
}
*/
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Button btn = (Button)e.Item.FindControl("btnDelete");
btn.Attributes.Add("onclick","javascript:return confirm('是否删除?');");
}
}
private void grdTest_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//if(e.CommandName == "Delete")
//{
//this.DeleteRow(this.grdTest.DataKeys[e.Item.ItemIndex].ToString());
//}
string i = this.grdTest.DataKeys[e.Item.ItemIndex].ToString();
string strConnection = "Data Source=127.0.0.1;Initial Catalog=pubs;User Id=sa;Password=123;";
SqlConnection myConnection = new SqlConnection(strConnection);
SqlCommand cmd = new SqlCommand("DELETE FROM bb WHERE (ID = "+i+")",myConnection);
myConnection.Open();
cmd.ExecuteNonQuery();
myConnection.Close();
grdTest.DataBind();
Response.Write("<script language=javascript>alert('删除成功');</script>");
//}
}
private void DeleteRow(string i)
{
string strConnection = "Data Source=127.0.0.1;Initial Catalog=pubs;User Id=sa;Password=123;";
SqlConnection myConnection = new SqlConnection(strConnection);
SqlCommand cmd = new SqlCommand("DELETE FROM bb WHERE (ID = "+i+")",myConnection);
myConnection.Open();
cmd.ExecuteNonQuery();
myConnection.Close();
grdTest.DataBind();
Response.Write("<script language=javascript>alert('删除成功');</script>");
}
private void Button1_Click(object sender, System.EventArgs e)
{
}
}
}