datagrid点删除,弹出一个确认和取消的消息框

有个简单的方法:
在datagrid的删除按扭
datagrid的属性生成器-》列-》添加按扭列-》删除
在数据字段中加上:
<div id="de" onclick="JavaScript:return confirm('确定删除吗?')">删除</div>
+++++++++++++++++++++++++++++++++++++++++++++++++++++++

如果按钮类型是LinkButton,则用ItemCreated事件:
protected void MyDataGrid_ItemCreated(object sender, DataGridItemEventArgs e)
{
LinkButton myDeleteButton;
switch(e.Item.ItemType)
{
case ListItemType.Item:
case ListItemType.AlternatingItem:
myDeleteButton = (LinkButton) e.Item.Cells[0].Controls[0];
myDeleteButton.Attributes.Add("onclick", @"return confirm('与此记录关联的所有记录均会被删除,你确认要删除吗?');");
break;
   }
  }
如果是PushButton,只需在代码区加上:
<script language="jscript">
function delete_confirm(e){
 if (event.srcElement.type=="submit" && document.all(event.srcElement.name).value =="删除" )
    event.returnValue =confirm("确认是否删除?");
}
document.onclick=delete_confirm;
</script>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

在DataGrid里面,要有一个这样的东东
<asp:TemplateColumn HeaderText="删除">
     <ItemTemplate>
 <asp:LinkButton id="btnDel" runat="server" CausesValidation="false" CommandName="Delete">
  <IMG alt="删除用户" src="pic/stoplogin.gif" align="absMiddle" border="0"></asp:LinkButton>
      </ItemTemplate>
</asp:TemplateColumn>
然后绑定几个事件
DataGrid1.ItemCreated+=new DataGridItemEventHandler(this.DataGrid1_ItemCreated);
DataGrid1.DeleteCommand+=new DataGridCommandEventHandler(this.DataGrid1_DeleteCommand);
然后
/// <summary>
/// 删除操作
/// </summary>
private void dgList_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
   你的删除操作!
}
/// <summary>
/// 删除操作确认
/// </summary>
private void grid_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
    ListItemType lit = e.Item.ItemType;
    if(lit == ListItemType.Item || lit == ListItemType.AlternatingItem)
    {
 LinkButton del = (LinkButton)e.Item.FindControl("btnDel");
 if(del==null)
 {
  return;
 }
 del.Attributes.Add("onclick","return confirm('确定要删除该纪录吗??')");
    }
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

第一步:在DATAGRID中添加一个按钮列。
第二步:完成对DATAGRID的绑定后,为删除按钮添加一个提示信息。
for(int i=0;i<this.DataGrid1.Items.Count;i++)
{
//这里的this.DataGrid1.Items[i].Controls[6].Controls[0];请根据你的DATAGRID实际情况设定。
LinkButton lb = (LinkButton)this.DataGrid1.Items[i].Controls[6].Controls[0];
lb.Attributes.Add("onclick","javascript:if(!window.confirm('确定要删除这条记录吗?')) return false;");
}
第三步:为按钮添加处理函数
private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//获得记录的编号
int recid = int.Parse(this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString());
if(….)//这里的条件判断已被雷神删除。
DeleteOperate (recid); //调删除函数
else
Response.Write("<script language=javascript>window.alert('没有删除记录权限!');history.go(-1);</script>");
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 

posted @ 2005-05-09 10:28  zhaogaojian  阅读(321)  评论(0编辑  收藏  举报