Default.aspx
View Code
<%@ 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>无标题页</title> </head> <body style="font-size: 12px"> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowDeleting="GridView1_RowDeleting" Style="border-right: #cccccc thin groove; border-top: #cccccc thin groove; border-left: #cccccc thin groove; border-bottom: #cccccc thin groove" Width="375px"> <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /> <Columns> <asp:BoundField DataField="员工编号" HeaderText="员工编号" /> <asp:BoundField DataField="员工姓名" HeaderText="员工姓名" /> <asp:BoundField DataField="身份证号" HeaderText="身份证号" /> <asp:BoundField DataField="联系电话" HeaderText="联系电话" /> <asp:CommandField ShowDeleteButton="True" /> </Columns> <RowStyle BackColor="#E3EAEB" /> <EditRowStyle BackColor="#7C6F57" /> <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" /> <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" /> <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /> <AlternatingRowStyle BackColor="White" /> </asp:GridView> </div> </form> </body> </html>
Default.aspx.cs
View Code
using System; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Data.SqlClient; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["strCon"]); SqlDataAdapter dap = new SqlDataAdapter("select * from 员工信息表", con); DataSet ds = new DataSet(); dap.Fill(ds, "table"); GridView1.DataSource = ds; GridView1.DataKeyNames = new string[] { "员工编号" }; GridView1.DataBind(); } protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["strCon"]);//创建数据库连接 con.Open();//打开数据库 SqlCommand cmd = new SqlCommand("procDeleteEmployee", con);//调用执行删除的存储过程 cmd.CommandType = CommandType.StoredProcedure;//设定数据操作类型 SqlParameter pares = new SqlParameter("@员工编号", SqlDbType.VarChar, 50); cmd.Parameters.Add(pares);//添加参数 cmd.Parameters["@员工编号"].Value = GridView1.DataKeys[e.RowIndex].Value.ToString();//传值操作 cmd.ExecuteNonQuery();//执行删除操作 con.Close();//关闭连接 this.Page_Load(sender, e); } }