1 A前台代码 2 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="Maticsoft.Web.CarRentWeb.test" %> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 4 5 6 <html xmlns="http://www.w3.org/1999/xhtml" > 7 <head runat="server"> 8 <title></title> 9 </head> 10 <body> 11 <form id="form1" runat="server"> 12 <div> 13 14 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 15 onpageindexchanging="GridView1_PageIndexChanging" 16 onrowcancelingedit="GridView1_RowCancelingEdit" 17 DataKeyNames="ID" 18 onrowdatabound="GridView1_RowDataBound" onrowdeleting="GridView1_RowDeleting" 19 onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"> 20 21 <Columns> 22 23 <asp:TemplateField HeaderText="编号(或用户名)"> 24 <ItemTemplate> 25 <asp:Label ID="Label1" runat="server" Text='<%# Eval("ID") %>'></asp:Label> 26 </ItemTemplate> 27 </asp:TemplateField> 28 <asp:TemplateField HeaderText="姓名"> 29 <EditItemTemplate> 30 <asp:TextBox ID="tbxName" Width="70px" runat="server" Text='<%# Eval("sname") %>'></asp:TextBox> 31 </EditItemTemplate> 32 <ItemTemplate> 33 <asp:Label ID="Label2" runat="server"><%# Eval("sname") %></asp:Label> 34 </ItemTemplate> 35 </asp:TemplateField> 36 <asp:TemplateField HeaderText="地址"> 37 38 39 <EditItemTemplate> 40 <asp:TextBox ID="tbxdizhi" Width="70px" runat="server" Text='<%# Eval("dizhi") %>'></asp:TextBox> 41 </EditItemTemplate> 42 43 <ItemTemplate> 44 <asp:Label ID="Label4" runat="server"><%# Eval("dizhi") %></asp:Label> 45 </ItemTemplate> 46 </asp:TemplateField> 47 48 49 <asp:CommandField ShowEditButton="True" HeaderText="编辑" /> 50 <asp:CommandField ShowDeleteButton="True" HeaderText="删除" /> 51 </Columns> 52 <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" /> 53 <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" /> 54 <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" /> 55 <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" /> 56 <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" /> 57 </asp:GridView> 58 </div> 59 </form> 60 </body> 61 </html>
B。后台代码 using System; using System.Collections; using System.Configuration; using System.Data; 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 BLL; using Model; using Maticsoft.Common; using System.Text.RegularExpressions; namespace Maticsoft.Web.CarRentWeb { public partial class test : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { GridViewBind(); } } private void GridViewBind() { BLL.testBll kk = new Maticsoft.BLL.testBll(); DataSet ds = kk.getall(); GridView1.DataSource = ds.Tables[0];//为GridView控件指名数据源 GridView1.DataBind();//GridView控件绑定数据 } // protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; GridViewBind(); } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { int i; //执行循环,保证每条数据都可以更新 for (i = 0; i < GridView1.Rows.Count; i++) { //首先判断是否是数据行 if (e.Row.RowType == DataControlRowType.DataRow) { //当鼠标停留时更改背景色 e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='Aqua'"); //当鼠标移开时还原背景色 e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c"); } } } protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; GridViewBind(); } protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { string userID = GridView1.DataKeys[e.RowIndex].Values[0].ToString(); //取出要删除记录的主键值 BLL.testBll kk = new Maticsoft.BLL.testBll(); Maticsoft.Model.testModel model = new Maticsoft.Model.testModel(); model.Id = Convert.ToInt32(userID); int k = kk.Del(model); if (k > 0)//根据主键使用DeleteByProc方法删除用户 { Response.Write("<script language=javascript>alert('删除成功!');location='test.aspx';</script>"); } else { Response.Write("<script language=javascript>alert('删除失败!');location='test.aspx';</script>"); } GridView1.EditIndex = -1; GridViewBind();//重新绑定数据 } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; //GridView编辑项索引等于单击行的索引 GridViewBind(); } protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { string userID = GridView1.DataKeys[e.RowIndex].Values[0].ToString(); //取出记录的主键值 Maticsoft.Model.testModel model = new Maticsoft.Model.testModel(); model .Name = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("tbxName")).Text; model.Id = Convert.ToInt32(userID); model.Dizhi = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("tbxdizhi")).Text; BLL.testBll kk = new Maticsoft.BLL.testBll(); int aa = kk.update(model); if (aa>0)//使用Usersmr类UpdateByProc方法修改用户信息 { GridViewBind(); Response.Write("<script language=javascript>alert('修改成功!');location='test.aspx';</script>"); } else { Response.Write("<script language=javascript>alert('修改成功!');location='test.aspx';</script>"); } GridView1.EditIndex = -1; GridViewBind(); } } }
1 C。sql语句 2 create table test 3 ( 4 id int primary key identity(1,1), 5 sname varchar(100), 6 dizhi varchar(100) 7 ) 8 go 9 insert into test values('张三','北京'); 10 insert into test values('李四','河北');