前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="lianxi1.aspx.cs" Inherits="WebApplication1.lianxi1" %>
<!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>
<style type="text/css">
.style1
{
width: 151px;
}
.style2
{
width: 176px;
}
.style3
{
width: 197px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server"
OnRowDeleting="GridView1_RowDeleting" DataKeyNames="pkID"
OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowEditing="GridView1_RowEditing"
style ="left:50px;vertical-align:middle;width:700px;height:200px;text-align:center"
AutoGenerateColumns="False" AllowPaging="true"
onpageindexchanging="GridView1_PageIndexChanging"
onrowcommand="GridView1_RowCommand">
<Columns>
<asp:CommandField ShowDeleteButton = "True" >
<ItemStyle Width="100px" />
</asp:CommandField>
<asp:CommandField ShowEditButton ="True" >
<ItemStyle Width="100px" />
</asp:CommandField>
<asp:BoundField HeaderText="姓名" DataField="Name" >
<ItemStyle Width="100px" />
</asp:BoundField>
<asp:BoundField HeaderText="性别" DataField="Sex" >
<ItemStyle Width="100px" />
</asp:BoundField>
<asp:BoundField HeaderText="住址" DataField="Address" >
<ItemStyle Width="200px" />
</asp:BoundField>
<asp:BoundField HeaderText="出生地" DataField="Born" >
<ItemStyle Width="200px" />
</asp:BoundField>
<asp:BoundField HeaderText="代码" DataField="Code" >
<ItemStyle Width="100px" />
</asp:BoundField>
</Columns>
<PagerTemplate>
<asp:Label ID="lblPage" runat="server" Text='<%# "第" + (((GridView)
Container.NamingContainer).PageIndex + 1) + "页/共" + (((GridView)
Container.NamingContainer).PageCount) + "页" %> '>
</asp:Label>
<asp:LinkButton ID="lbnFirst" runat="Server" Text="首页" Enabled='<%# ((GridView)
Container.NamingContainer).PageIndex != 0 %>' CommandName="Page" CommandArgument="First" >
</asp:LinkButton>
<asp:LinkButton ID="lbnPrev" runat="server" Text="上一页" Enabled='<%# ((GridView)
Container.NamingContainer).PageIndex != 0 %>' CommandName="Page" CommandArgument="Prev" >
</asp:LinkButton>
<asp:LinkButton ID="lbnNext" runat="Server" Text="下一页" Enabled='<%# ((GridView)
Container.NamingContainer).PageIndex != (((GridView)
Container.NamingContainer).PageCount - 1) %>' CommandName="Page" CommandArgument="Next" >
</asp:LinkButton>
<asp:LinkButton ID="lbnLast" runat="Server" Text="尾页" Enabled='<%# ((GridView)
Container.NamingContainer).PageIndex != (((GridView)
Container.NamingContainer).PageCount - 1) %>' CommandName="Page" CommandArgument="Last" >
</asp:LinkButton>
到第
<asp:TextBox runat="server" ID="inPageNum">
</asp:TextBox>页
<asp:Button ID="Button4" CommandName="go" runat="server" text="go"/>
</PagerTemplate>
</asp:GridView>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="添加" />
<br />
<br />
<div >
<table cellpadding="2" cellspacing="2" width="980px" border="true"
frame="border" >
<tr>
<td align="center" colspan="6">
<span lang="zh-cn">信息录入</span></td>
</tr>
<tr>
<td >
<span lang="zh-cn">姓名</span></td>
<td class="style1" >
<asp:TextBox ID="TBName" runat="server"></asp:TextBox>
</td>
<td >
<span lang="zh-cn">性别</span></td>
<td class="style2" >
<asp:TextBox ID="TBSex" runat="server"></asp:TextBox>
</td>
<td >
<span lang="zh-cn">住址</span></td>
<td class="style3">
<asp:TextBox ID="TBAddress" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style7">
<span lang="zh-cn">出生地</span></td>
<td colspan="2">
<asp:TextBox ID="TBBorn" runat="server"></asp:TextBox></td>
<td colspan="2">
<span lang="zh-cn">代码</span></td>
<td class="style3">
<asp:TextBox ID="TBCode" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td colspan="6">
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="添加" /></td>
</tr>
</table>
</div>
<div>
请输入pkID进行查询:<br />
<asp:TextBox ID="TBpkID1" runat="server" Height="17px" Width="154px"></asp:TextBox><br />
<asp:Button ID="Button3" runat="server" Text=" 查询" OnClick="Button3_Click" /><br />
<asp:GridView ID="GridView2" runat="server" Style =" left:50px;vertical-align:middle;width:500px;height:50px;text-align:center">
</asp:GridView>
</div>
</form>
</body>
</html>
后台:
using System;
using System.Collections;
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;
namespace WebApplication1
{
public partial class lianxi1 : System.Web.UI.Page
{
public string StrConnection = "data source=ASCZ-007\\SQLEXPRESS;User Id=sa;Password=sa123;packet size=4096;database=zsgc";
SqlConnection conn;
SqlCommand cmd;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
binddata();
}
}
protected void binddata()
{
conn = new SqlConnection(StrConnection);
conn.Open();
String sql = "select * from People";
SqlDataAdapter myda = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
myda.Fill(ds, "People");
GridView1.DataSource = ds.Tables["People"].DefaultView;
GridView1.DataBind();
conn.Close();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
conn = new SqlConnection(StrConnection);
conn.Open();
String id = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
int idt = int.Parse(id);
cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "delete from People where pkID = " + idt;
cmd.ExecuteNonQuery();
conn.Close();
binddata();
}
protected void Button1_Click(object sender, EventArgs e)
{
// Response.Redirect("add.aspx");
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string id = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
string pkid = GridView1.Rows[e.RowIndex].Cells[2].Text.ToString();
string name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text.Trim();
string sex = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text.Trim();
string address = ((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text.Trim();
string born = ((TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).Text.Trim();
string code = ((TextBox)GridView1.Rows[e.RowIndex].Cells[7].Controls[0]).Text.Trim();
string SqlStr = "update People set pkID='" + pkid + "',Name='" + name + "',Sex='" + sex + "',Address='" + address + "',Born='" + born + "',Code='" + code + "' where pkID=" + pkid;
conn = new SqlConnection(StrConnection);
conn.Open();
cmd = new SqlCommand(SqlStr, conn);
cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Close();
GridView1.EditIndex = -1;
binddata();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
binddata();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
binddata();
}
protected void Button2_Click(object sender, EventArgs e)
{
conn = new SqlConnection(StrConnection);
conn.Open();
SqlCommand cmd = new SqlCommand("insert into People(Name,Sex,Address,Born,Code) values(@Name,@Sex,@Address,@Born,@Code)", conn);
//cmd.Connection = conn;
// cmd.Parameters.Add("@pkID", SqlDbType.VarChar);
cmd.Parameters.Add("@Name", SqlDbType.VarChar);
cmd.Parameters.Add("@Sex", SqlDbType.VarChar);
cmd.Parameters.Add("@Address", SqlDbType.VarChar);
cmd.Parameters.Add("@Born", SqlDbType.VarChar);
cmd.Parameters.Add("@Code", SqlDbType.VarChar);
// cmd.Parameters["@pkID"].Value = TBpkID.Text;
cmd.Parameters["@Name"].Value = TBName.Text;
cmd.Parameters["@Sex"].Value = TBSex.Text;
cmd.Parameters["@Address"].Value = TBAddress.Text;
cmd.Parameters["@Born"].Value = TBBorn.Text;
cmd.Parameters["@Code"].Value = TBCode.Text;
cmd.ExecuteNonQuery();
conn.Close();
binddata();
init();
}
protected void init()
{
// TBpkID.Text = "";
// TBpkID.Focus();
TBName.Text = "";
TBName.Focus();
TBSex.Text = "";
TBAddress.Text = "";
TBBorn.Text = "";
TBCode.Text = "";
}
protected void Button3_Click(object sender, EventArgs e)
{
conn = new SqlConnection(StrConnection);
conn.Open();
string id = TBpkID1.Text;
String sql = "select * from People where pkID = " + id;
SqlDataAdapter myda = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
myda.Fill(ds, "People");
GridView2.DataSource = ds.Tables["People"].DefaultView;
GridView2.DataBind();
conn.Close();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
try
{
GridView1.PageIndex = e.NewPageIndex;
binddata();
TextBox tb = (TextBox)GridView1.BottomPagerRow.FindControl("inPageNum");
tb.Text = (GridView1.PageIndex + 1).ToString();
}
catch
{
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "go")
{
try
{
TextBox tb = (TextBox)GridView1.BottomPagerRow.FindControl("inPageNum");
int num = Int32.Parse(tb.Text);
GridViewPageEventArgs ea = new GridViewPageEventArgs(num - 1);
GridView1_PageIndexChanging(null, ea);
}
catch
{
}
}
}
}
}
效果图:
数据库: