GridViewEdit.aspx
GridViewEdit.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewEditTest.aspx.cs" Inherits="GridViewEditTest" %>
<!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 runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Panel ID="pnlEdit" runat="server" Height="50px" Width="684px" Visible="False">
姓名:<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>地址:<asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>邮编<asp:TextBox ID="txtPostCode" runat="server"></asp:TextBox><asp:Button ID="btnOK" runat="server" Text="确定" OnClick="btnOK_Click" />
<asp:Button ID="btnCancel" runat="server" Text="取消" OnClick="btnCancel_Click" /></asp:Panel>
<br />
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White"
BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataKeyNames="CLientID"
GridLines="Horizontal" OnPageIndexChanging="GridView1_PageIndexChanging" OnSelectedIndexChanging="GridView1_SelectedIndexChanging" AllowPaging="True">
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="ClientName" HeaderText="姓名" SortExpression="ClientName" />
<asp:BoundField DataField="AddressStr" HeaderText="地址" SortExpression="AddressStr" />
<asp:BoundField DataField="PostCode" HeaderText="邮编" SortExpression="PostCode" />
</Columns>
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
<AlternatingRowStyle BackColor="#F7F7F7" />
</asp:GridView>
</div>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewEditTest.aspx.cs" Inherits="GridViewEditTest" %>
<!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 runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Panel ID="pnlEdit" runat="server" Height="50px" Width="684px" Visible="False">
姓名:<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>地址:<asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>邮编<asp:TextBox ID="txtPostCode" runat="server"></asp:TextBox><asp:Button ID="btnOK" runat="server" Text="确定" OnClick="btnOK_Click" />
<asp:Button ID="btnCancel" runat="server" Text="取消" OnClick="btnCancel_Click" /></asp:Panel>
<br />
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White"
BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataKeyNames="CLientID"
GridLines="Horizontal" OnPageIndexChanging="GridView1_PageIndexChanging" OnSelectedIndexChanging="GridView1_SelectedIndexChanging" AllowPaging="True">
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="ClientName" HeaderText="姓名" SortExpression="ClientName" />
<asp:BoundField DataField="AddressStr" HeaderText="地址" SortExpression="AddressStr" />
<asp:BoundField DataField="PostCode" HeaderText="邮编" SortExpression="PostCode" />
</Columns>
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
<AlternatingRowStyle BackColor="#F7F7F7" />
</asp:GridView>
</div>
</form>
</body>
</html>
ClientInfo.cs
ClientInfo.cs
using System;
using System.Data;
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;
/// <summary>
/// ClientInfo 的摘要说明
/// </summary>
public class ClientInfo
{
private int clientID;//客户编号
public int ClientID
{
get { return clientID; }
set { clientID = value; }
}
private string clientName;//客户姓名
public string ClientName
{
get { return clientName; }
set { clientName = value; }
}
private string addressStr;//客户地址
public string AddressStr
{
get { return addressStr; }
set { addressStr = value; }
}
private string postCode;//客户邮编
public string PostCode
{
get { return postCode; }
set { postCode = value; }
}
private string telephone;//客户电话
public string Telephone
{
get { return telephone; }
set { telephone = value; }
}
private string email;//客户邮件
public string Email
{
get { return email; }
set { email = value; }
}
}
using System;
using System.Data;
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;
/// <summary>
/// ClientInfo 的摘要说明
/// </summary>
public class ClientInfo
{
private int clientID;//客户编号
public int ClientID
{
get { return clientID; }
set { clientID = value; }
}
private string clientName;//客户姓名
public string ClientName
{
get { return clientName; }
set { clientName = value; }
}
private string addressStr;//客户地址
public string AddressStr
{
get { return addressStr; }
set { addressStr = value; }
}
private string postCode;//客户邮编
public string PostCode
{
get { return postCode; }
set { postCode = value; }
}
private string telephone;//客户电话
public string Telephone
{
get { return telephone; }
set { telephone = value; }
}
private string email;//客户邮件
public string Email
{
get { return email; }
set { email = value; }
}
}
ClientInfoAccessObj.cs
ClientInfoAccessObj.cs
using System;
using System.Data;
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;
using System.Collections.Generic;
using System.Data.SqlClient;
/// <summary>
/// ClientInfoAccessObj 的摘要说明
/// </summary>
public class ClientInfoAccessObj
{
//连接字符串
private readonly string connString = "server=.\\sqlexpress;database=Clients;uid=sa;pwd=123456;";
//获得所有的客户信息
public List<ClientInfo> GetAllClients()
{
List<ClientInfo> clients = new List<ClientInfo>();
using (SqlConnection conn=new SqlConnection(connString))
{
string sql = "select * from orderClient";
using (SqlCommand comm=new SqlCommand(sql,conn))
{
conn.Open();
using (SqlDataReader sdr=comm.ExecuteReader())
{
while (sdr.Read())
{
ClientInfo ci = new ClientInfo();
ci.ClientID = int.Parse(sdr["ClientID"].ToString());
ci.ClientName = sdr["ClientName"].ToString();
ci.AddressStr = sdr["AddressStr"].ToString();
ci.PostCode = sdr["PostCode"].ToString();
ci.Telephone = sdr["Telephone"].ToString();
ci.Email = sdr["Email"].ToString();
clients.Add(ci);
}
return clients;
}
}
}
}
//根据客户ID删除客户信息
public int DeleteClientInfoForID(int ClientID)
{
using (SqlConnection conn=new SqlConnection(connString))
{
string sql = "delete from orderClient where ClientID=@ClientID";
using (SqlCommand comm=new SqlCommand(sql,conn))
{
conn.Open();
comm.Parameters.AddWithValue("@ClientID",ClientID);
int result = comm.ExecuteNonQuery();
return result;
}
}
}
//根据客户姓名查询客户信息
public List<ClientInfo> FindClientInfo(string ClientName)
{
List<ClientInfo> clients = new List<ClientInfo>();
using (SqlConnection conn=new SqlConnection(connString))
{
string sql = "select * from orderClient where ClientName Like '%'+@ClientName+'%'";
using (SqlCommand comm=new SqlCommand(sql,conn))
{
conn.Open();
comm.Parameters.AddWithValue("@ClientName",ClientName);
using (SqlDataReader sdr=comm.ExecuteReader())
{
while (sdr.Read())
{
ClientInfo ci = new ClientInfo();
ci.ClientID = int.Parse(sdr["ClientID"].ToString());
ci.ClientName = sdr["ClientName"].ToString();
ci.AddressStr = sdr["AddressStr"].ToString();
ci.PostCode = sdr["PostCode"].ToString();
ci.Telephone = sdr["Telephone"].ToString();
ci.Email = sdr["Email"].ToString();
clients.Add(ci);
}
return clients;
}
}
}
}
//更新客户信息
public int UpdateClientInfo(ClientInfo client)
{
using (SqlConnection conn=new SqlConnection(connString))
{
string sql = "update orderClient set ClientName=@ClientName,PostCode=@PostCode,AddressStr=@AddressStr where ClientID=@ClientID";
using (SqlCommand comm=new SqlCommand(sql,conn))
{
conn.Open();
comm.Parameters.AddWithValue("@ClientName",client.ClientName);
comm.Parameters.AddWithValue("@PostCode", client.PostCode);
comm.Parameters.AddWithValue("@AddressStr", client.AddressStr);
comm.Parameters.AddWithValue("@ClientID", client.ClientID);
int result = comm.ExecuteNonQuery();
return result;
}
}
}
}
using System;
using System.Data;
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;
using System.Collections.Generic;
using System.Data.SqlClient;
/// <summary>
/// ClientInfoAccessObj 的摘要说明
/// </summary>
public class ClientInfoAccessObj
{
//连接字符串
private readonly string connString = "server=.\\sqlexpress;database=Clients;uid=sa;pwd=123456;";
//获得所有的客户信息
public List<ClientInfo> GetAllClients()
{
List<ClientInfo> clients = new List<ClientInfo>();
using (SqlConnection conn=new SqlConnection(connString))
{
string sql = "select * from orderClient";
using (SqlCommand comm=new SqlCommand(sql,conn))
{
conn.Open();
using (SqlDataReader sdr=comm.ExecuteReader())
{
while (sdr.Read())
{
ClientInfo ci = new ClientInfo();
ci.ClientID = int.Parse(sdr["ClientID"].ToString());
ci.ClientName = sdr["ClientName"].ToString();
ci.AddressStr = sdr["AddressStr"].ToString();
ci.PostCode = sdr["PostCode"].ToString();
ci.Telephone = sdr["Telephone"].ToString();
ci.Email = sdr["Email"].ToString();
clients.Add(ci);
}
return clients;
}
}
}
}
//根据客户ID删除客户信息
public int DeleteClientInfoForID(int ClientID)
{
using (SqlConnection conn=new SqlConnection(connString))
{
string sql = "delete from orderClient where ClientID=@ClientID";
using (SqlCommand comm=new SqlCommand(sql,conn))
{
conn.Open();
comm.Parameters.AddWithValue("@ClientID",ClientID);
int result = comm.ExecuteNonQuery();
return result;
}
}
}
//根据客户姓名查询客户信息
public List<ClientInfo> FindClientInfo(string ClientName)
{
List<ClientInfo> clients = new List<ClientInfo>();
using (SqlConnection conn=new SqlConnection(connString))
{
string sql = "select * from orderClient where ClientName Like '%'+@ClientName+'%'";
using (SqlCommand comm=new SqlCommand(sql,conn))
{
conn.Open();
comm.Parameters.AddWithValue("@ClientName",ClientName);
using (SqlDataReader sdr=comm.ExecuteReader())
{
while (sdr.Read())
{
ClientInfo ci = new ClientInfo();
ci.ClientID = int.Parse(sdr["ClientID"].ToString());
ci.ClientName = sdr["ClientName"].ToString();
ci.AddressStr = sdr["AddressStr"].ToString();
ci.PostCode = sdr["PostCode"].ToString();
ci.Telephone = sdr["Telephone"].ToString();
ci.Email = sdr["Email"].ToString();
clients.Add(ci);
}
return clients;
}
}
}
}
//更新客户信息
public int UpdateClientInfo(ClientInfo client)
{
using (SqlConnection conn=new SqlConnection(connString))
{
string sql = "update orderClient set ClientName=@ClientName,PostCode=@PostCode,AddressStr=@AddressStr where ClientID=@ClientID";
using (SqlCommand comm=new SqlCommand(sql,conn))
{
conn.Open();
comm.Parameters.AddWithValue("@ClientName",client.ClientName);
comm.Parameters.AddWithValue("@PostCode", client.PostCode);
comm.Parameters.AddWithValue("@AddressStr", client.AddressStr);
comm.Parameters.AddWithValue("@ClientID", client.ClientID);
int result = comm.ExecuteNonQuery();
return result;
}
}
}
}
GridViewEdit.aspx.cs
GirdViewEdit.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 GridViewEditTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ClientInfoAccessObj accessor = new ClientInfoAccessObj();
GridView1.DataSource = accessor.GetAllClients();
GridView1.PageIndex = 0;
GridView1.DataBind();
}
}
protected void btnOK_Click(object sender, EventArgs e)
{
ClientInfo client = new ClientInfo();
client.ClientID = (int)ViewState["ClientID"];
client.ClientName = txtUserName.Text;
client.AddressStr = txtAddress.Text;
client.PostCode = txtPostCode.Text;
ClientInfoAccessObj accessor = new ClientInfoAccessObj();
accessor.UpdateClientInfo(client);//更新客户信息
ClientScript.RegisterClientScriptBlock(this.GetType(), "Info", "alert('记录已成功更新');", true);
pnlEdit.Visible = false;
GridView1.DataSource = accessor.GetAllClients();
GridView1.DataBind();
}
protected void btnCancel_Click(object sender, EventArgs e)
{
pnlEdit.Visible = false;//设置编辑区域不可见
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
//当点击选择列时触发该事件
pnlEdit.Visible = true;//显示编辑区域
txtUserName.Text = GridView1.Rows[e.NewSelectedIndex].Cells[1].Text;//获得选中当前行的客户姓名显示在编辑区域
txtAddress.Text = GridView1.Rows[e.NewSelectedIndex].Cells[2].Text;//获得选中当前行的客户地址显示在编辑区域
txtPostCode.Text = GridView1.Rows[e.NewSelectedIndex].Cells[3].Text;//获得选中当前行的客户邮编显示在编辑区域
ViewState["ClientID"] = GridView1.DataKeys[e.NewSelectedIndex].Value;//根据点击的当前行的索引获得对应的主键列保存到ViewState
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
//分页显示
ClientInfoAccessObj accessor = new ClientInfoAccessObj();
GridView1.DataSource = accessor.GetAllClients();
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
}
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 GridViewEditTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ClientInfoAccessObj accessor = new ClientInfoAccessObj();
GridView1.DataSource = accessor.GetAllClients();
GridView1.PageIndex = 0;
GridView1.DataBind();
}
}
protected void btnOK_Click(object sender, EventArgs e)
{
ClientInfo client = new ClientInfo();
client.ClientID = (int)ViewState["ClientID"];
client.ClientName = txtUserName.Text;
client.AddressStr = txtAddress.Text;
client.PostCode = txtPostCode.Text;
ClientInfoAccessObj accessor = new ClientInfoAccessObj();
accessor.UpdateClientInfo(client);//更新客户信息
ClientScript.RegisterClientScriptBlock(this.GetType(), "Info", "alert('记录已成功更新');", true);
pnlEdit.Visible = false;
GridView1.DataSource = accessor.GetAllClients();
GridView1.DataBind();
}
protected void btnCancel_Click(object sender, EventArgs e)
{
pnlEdit.Visible = false;//设置编辑区域不可见
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
//当点击选择列时触发该事件
pnlEdit.Visible = true;//显示编辑区域
txtUserName.Text = GridView1.Rows[e.NewSelectedIndex].Cells[1].Text;//获得选中当前行的客户姓名显示在编辑区域
txtAddress.Text = GridView1.Rows[e.NewSelectedIndex].Cells[2].Text;//获得选中当前行的客户地址显示在编辑区域
txtPostCode.Text = GridView1.Rows[e.NewSelectedIndex].Cells[3].Text;//获得选中当前行的客户邮编显示在编辑区域
ViewState["ClientID"] = GridView1.DataKeys[e.NewSelectedIndex].Value;//根据点击的当前行的索引获得对应的主键列保存到ViewState
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
//分页显示
ClientInfoAccessObj accessor = new ClientInfoAccessObj();
GridView1.DataSource = accessor.GetAllClients();
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
}