- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head id="Head1" runat="server">
- <title>GridView实现删除时弹出确认对话框</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="10" ForeColor="#333333" OnRowDeleting="GridView1_RowDeleting" PageSize="3" OnRowDataBound="GridView1_RowDataBound">
- <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
- <Columns>
- <asp:BoundField DataField="ID" HeaderText="用户ID" ReadOnly="True" />
- <asp:BoundField DataField="Name" HeaderText="用户姓名" />
- <asp:BoundField DataField="Sex" HeaderText="性别" />
- <asp:BoundField DataField="Address" HeaderText="家庭住址" />
- <asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
- </Columns>
- <RowStyle ForeColor="#000066" />
- <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
- <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
- <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
- </asp:GridView>
- </div>
- </form>
- </body>
- </html>
- using System;
- using System.Data;
- using System.Data.SqlClient;
- using System.Configuration;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- public partial class _Default : System.Web.UI.Page
- {
- SqlConnection sqlConn;
- SqlCommand sqlComm;
- string strConn = "Data Source=(local);Database=Exercise;Uid=sa;Pwd=sa";
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- databind();
- }
- }
- protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
- {
- string strSql = "delete myDt where id = '" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
- sqlConn = new SqlConnection(strConn);
- sqlComm = new SqlCommand(strSql, sqlConn);
- sqlConn.Open();
- sqlComm.ExecuteNonQuery();
- sqlConn.Close();
- databind();
- }
- protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
- {
- if (e.Row.RowType == DataControlRowType.DataRow)
- {
- if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
- {
- ((LinkButton)e.Row.Cells[4].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除:/"" + e.Row.Cells[1].Text + "/"吗?')");
- }
- }
- }
- private void databind()
- {
- string strSql = "select * from myDt";
- sqlConn = new SqlConnection(strConn);
- SqlDataAdapter sqlDa = new SqlDataAdapter(strSql, sqlConn);
- DataSet myDs = new DataSet();
- sqlConn.Open();
- sqlDa.Fill(myDs, "myDt");
- GridView1.DataSource = myDs;
- GridView1.DataKeyNames = new string[] { "ID" };//主键
- GridView1.DataBind();
- sqlConn.Close();
- }
- }