日期:2014-05-19  浏览次数:20705 次

怎样在DataGrid列的删除按钮上加一个确认对话框
我用的是c#vs2003,这是那段代码,
private   void   DataGrid1_DeleteCommand(object   source,   System.Web.UI.WebControls.DataGridCommandEventArgs   e)
{
if(Session[ "qx "].ToString().IndexOf( "系统管理员 ")==-1)
Response.Write( " <Script   language=javascript>   alert( '对不起,你无权限删除! ');   </Script> ");
else

{
clsqlcon=new   SqlConnection(strConn);
clsqlcon.Open();
str= "delete   from   人员   where   标识= "+DataGrid1.DataKeys[e.Item.ItemIndex].ToString().Trim();
clsqlcom=new   SqlCommand(str,clsqlcon);
clsqlcom.ExecuteNonQuery();
clsqlcon.Dispose();
clBindGrid();
Response.Redirect( "yyxg.aspx ");
Response.Redirect( "mulu.aspx ");
}
}

------解决方案--------------------
可以在datagrid 的databound事件中写如

Button BtnDelete=(Button)(e.Item.FindControl( "BtnDelete "));
BtnDelete.Attributes[ "onclick "]= "return confirm( 'Are you sure delete this record? ') ";
------解决方案--------------------
在你的datagrid的itemdatabound事件上加如下代碼:

if((e.Item.ItemType==ListItemType.AlternatingItem)|| e.Item.ItemType==ListItemType.Item))
{
Button BtnDelete = (Button)e.Item.FindControl( "button1 ");
lbtn.Attributes.Add( "onclick ", "javascript:return confirm( 'Confirm delete? ') ");
}

------解决方案--------------------
--button1是你的delete button的id:

if((e.Item.ItemType==ListItemType.AlternatingItem)|| e.Item.ItemType==ListItemType.Item))
{
Button BtnDelete = (Button)e.Item.FindControl( "button1 ");
BtnDelete .Attributes.Add( "onclick ", "javascript:return confirm( 'Confirm delete? ') ");
}
------解决方案--------------------
上面的代码都是可以的,帮顶,楼主还是仔细查查自己的代码吧