要把查询语句记录下来
前台代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>考勤信息</h2>
<asp:TextBox ID="TextBox1" runat="server" placeholder="员工工号"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="查询" OnClick="Button1_Click" />
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" BackColor="White"
PageSize="5" onpageindexchanging="GridView1_PageIndexChanging"
Width="1080px">
<FooterStyle BackColor="White" ForeColor="#000066" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ID" HeaderText="编号" SortExpression="ID" />
<asp:BoundField DataField="num" HeaderText="工号" SortExpression="num" />
<asp:BoundField DataField="date" HeaderText="日期" SortExpression="date"/>
<asp:BoundField DataField="type" HeaderText="考勤" SortExpression="type"/>
</Columns>
<RowStyle HorizontalAlign ="Center" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Right" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
</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.Sql;
using System.Data.SqlClient;
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["search"] = null;
LoadGridview();//加载考勤明细表
}
}
private void LoadGridview()
{
//考勤明细表
SqlConnection sqlcon3 = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
sqlcon3.Open();
string str1 = "select * from kaoqin order by ID desc";
SqlDataAdapter adsa3 = new SqlDataAdapter(str1, sqlcon3);
DataSet adds3 = new DataSet();
adsa3.Fill(adds3);
GridView1.DataSource = adds3;
GridView1.DataKeyNames = new string[] { "ID" };
GridView1.DataBind();
sqlcon3.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
ViewState["search"] = 1;
LoadSearchGridview(TextBox1.Text);
}
private void LoadSearchGridview(string search)
{
string strSearch = null;
if (TextBox1.Text != "")
{
strSearch = "select * from kaoqin where num='" + search + "' order by ID desc";
}
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
sqlcon.Open();
SqlDataAdapter adsa = new SqlDataAdapter(strSearch, sqlcon);
DataSet adds = new DataSet();
adsa.Fill(adds);
this. GridView1.DataSource = adds;
this.GridView1.DataKeyNames = new string[] { "ID" };
this. GridView1.DataBind();
sqlcon.Close();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
if (ViewState["search"] == null)
{
LoadGridview();//加载考勤明细表
}
else {
LoadSearchGridview(TextBox1.Text); //加载查询后的表
}
}
}