日期:2014-05-18  浏览次数:20474 次

<asp:ButtonField CommandName="Delete" Text="删除" />如何添加删除对话框
<asp:GridView ID="grid" runat="server" DataKeyNames="DepartmentID" Width="100%" 
  AutoGenerateColumns="False" onrowcancelingedit="grid_RowCancelingEdit" 
  onrowdeleting="grid_RowDeleting" onrowediting="grid_RowEditing" 
  onrowupdating="grid_RowUpdating">
  <Columns>
  <asp:BoundField DataField="Name" HeaderText="Department Name" />
  <asp:BoundField DataField="Description" HeaderText="Department Description" />
  <asp:HyperLinkField DataNavigateUrlFields="DepartmentID" 
  DataNavigateUrlFormatString="AdminCategories.aspx?DepartmentID={0}" 
  HeaderText="View Categories" Text="View Categories" />
  <asp:CommandField ShowEditButton="True" />
  <asp:ButtonField CommandName="Delete" Text="删除" />
  </Columns>
</asp:GridView>

protected void grid_RowDeleting(object sender, GridViewDeleteEventArgs e)
  {
  //获取将要删除的记录ID
  string id = grid.DataKeys[e.RowIndex].Value.ToString();
  //执行delete命令
  bool success = CatalogAccess.DeleteDepartment(id);
  //取消编辑模式
  grid.EditIndex = -1;
  //显示状态信息
  statusLabel.Text = success ? "删除成功" : "删除失败";
  //重载该网格
  BindGrid();
  }

怎么添加删除对话框,求帮助

------解决方案--------------------
添加方法
http://dotnet.aspx.cc/file/Add-confirm-dialog-with-ButtonField-Column-of-GridView.aspx

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Button b = e.Row.Cells[0].Controls[0] as Button;
b.Attributes.Add("onclick", "if(confirm('你真的要删除吗?')){ __doPostBack('GridView1','$" + e.Row.RowIndex + "')}else{return false;}");
}
}
------解决方案--------------------
C# code

<asp:TemplateField HeaderText="Delete" ShowHeader="true">
<ItemTemplate>
<asp:Button ID="BtnDelete" runat="server" Text="Delete" CommandName="Delete" OnClientClick='<%#"if(!confirm(\"Do you confirm to delete it?\"))return false;" %>' />
</ItemTemplate>                                                       
</asp:TemplateField>