GridView实现删除时弹出确认对话框

  1. <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %> 
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  3. <html xmlns="http://www.w3.org/1999/xhtml" > 
  4. <head id="Head1" runat="server"> 
  5.     <title>GridView实现删除时弹出确认对话框</title> 
  6. </head> 
  7. <body> 
  8.     <form id="form1" runat="server"> 
  9.     <div> 
  10.         <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="10" ForeColor="#333333" OnRowDeleting="GridView1_RowDeleting" PageSize="3" OnRowDataBound="GridView1_RowDataBound">  
  11.             <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> 
  12.             <Columns> 
  13.                 <asp:BoundField DataField="ID" HeaderText="用户ID" ReadOnly="True" /> 
  14.                 <asp:BoundField DataField="Name" HeaderText="用户姓名" /> 
  15.                 <asp:BoundField DataField="Sex" HeaderText="性别" /> 
  16.                 <asp:BoundField DataField="Address" HeaderText="家庭住址" /> 
  17.                 <asp:CommandField HeaderText="删除" ShowDeleteButton="True" /> 
  18.             </Columns> 
  19.             <RowStyle ForeColor="#000066" /> 
  20.             <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" /> 
  21.             <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" /> 
  22.             <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" /> 
  23.         </asp:GridView> 
  24.     </div> 
  25.     </form> 
  26. </body> 
  27. </html>
  1. using System;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4. using System.Configuration;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. public partial class _Default : System.Web.UI.Page
  12. {
  13.     SqlConnection sqlConn;
  14.     SqlCommand sqlComm;
  15.     string strConn = "Data Source=(local);Database=Exercise;Uid=sa;Pwd=sa";
  16.     protected void Page_Load(object sender, EventArgs e)
  17.     {
  18.         if (!IsPostBack)
  19.         {
  20.             databind();
  21.         }
  22.     }
  23.     protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
  24.     {
  25.         string strSql = "delete myDt where id = '" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
  26.         sqlConn = new SqlConnection(strConn);
  27.         sqlComm = new SqlCommand(strSql, sqlConn);
  28.         sqlConn.Open();
  29.         sqlComm.ExecuteNonQuery();
  30.         sqlConn.Close();
  31.         databind();
  32.     }
  33.     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  34.     {
  35.         if (e.Row.RowType == DataControlRowType.DataRow)
  36.         {
  37.             if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
  38.             {
  39.                 ((LinkButton)e.Row.Cells[4].Controls[0]).Attributes.Add("onclick""javascript:return confirm('你确认要删除:/"" + e.Row.Cells[1].Text + "/"吗?')");
  40.             }
  41.         } 
  42.     } 
  43.     private void databind()
  44.     {
  45.         string strSql = "select * from myDt";
  46.         sqlConn = new SqlConnection(strConn);
  47.         SqlDataAdapter sqlDa = new SqlDataAdapter(strSql, sqlConn);
  48.         DataSet myDs = new DataSet();
  49.         sqlConn.Open();
  50.         sqlDa.Fill(myDs, "myDt");
  51.         GridView1.DataSource = myDs;
  52.         GridView1.DataKeyNames = new string[] { "ID" };//主键 
  53.         GridView1.DataBind();
  54.         sqlConn.Close();
  55.     }
  56. }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值