NET 查询再分页
页面文件
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="show.aspx.cs" Inherits="addressbook_show" Debug="true" %>
<!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>
<link rel="stylesheet" rev="stylesheet" href="../css/style.css" type="text/css" media="all"/>
<script language="javascript" type="text/javascript">
// 鼠标经过改变行的颜色
if (!objbeforeItem)
{
var objbeforeItem=null;
var objbeforeItembackgroundColor=null;
}
function ItemOver(obj)
{
if(objbeforeItem)
{
objbeforeItem.style.backgroundColor = objbeforeItembackgroundColor;
}
objbeforeItembackgroundColor = obj.style.backgroundColor;
objbeforeItem = obj;
obj.style.backgroundColor = "#B9D1F3";
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<center><asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" OnClick="btnSearch_Click" Text="查 询" class=btn3_mouseout onmouseover="this.className='btn3_mouseover'"
onmouseout="this.className='btn3_mouseout'" onmousedown="this.className='btn3_mousedown'" onmouseup="this.className='btn3_mouseup'" style="width: 60px"/></center><br>
<table>
<tr><td>
<asp:GridView ID="gvhtel" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" AllowPaging="true" PageSize="20" PagerSettings-Visible="false" OnRowDataBound="gvhtel_RowDataBound">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:BoundField DataField="code" HeaderText="工号" HeaderStyle-Width="50px"/>
<asp:BoundField DataField="name" HeaderText="姓名" HeaderStyle-Width="100px"/>
<asp:BoundField DataField="department" HeaderText="科室" HeaderStyle-Width="100px"/>
<asp:BoundField DataField="tel" HeaderText="宅电" HeaderStyle-Width="70px"/>
<asp:BoundField DataField="mtel" HeaderText="手机" HeaderStyle-Width="70px"/>
<asp:BoundField DataField="xlt" HeaderText="小灵通" HeaderStyle-Width="70px"/>
<asp:BoundField DataField="htel" HeaderText="院内电话" HeaderStyle-Width="70px"/>
<asp:BoundField DataField="floor" HeaderText="楼层" HeaderStyle-Width="50px"/>
<asp:BoundField DataField="hmail" HeaderText="院内邮件" HeaderStyle-Width="80px"/>
<asp:BoundField DataField="mail" HeaderText="MSN" HeaderStyle-Width="50px"/>
<asp:BoundField DataField="qq" HeaderText="QQ" HeaderStyle-Width="50px"/>
</Columns>
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<EditRowStyle BackColor="#999999" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
</td></tr>
<tr><td>
<asp:LinkButton ID="btnFirst" CommandArgument="first" OnClick="PagerButtonClick"
runat="server">首 页</asp:LinkButton> <asp:LinkButton ID="btnPrev" CommandArgument="prev" OnClick="PagerButtonClick" runat="server">上一页</asp:LinkButton>
<asp:LinkButton ID="btnNext" CommandArgument="next" OnClick="PagerButtonClick" runat="server">下一页</asp:LinkButton>
<asp:LinkButton ID="btnLast" CommandArgument="last" OnClick="PagerButtonClick" runat="server">尾 页</asp:LinkButton>
<asp:Label ID="LblCurrentIndex" runat="server"></asp:Label>
<asp:Label ID="LblPageCount" runat="server"></asp:Label>
<asp:Label ID="LblRecordCount" runat="server"></asp:Label>
跳转到第<asp:DropDownList ID="ddlCurrentPage" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>页
</td></tr>
</table>
</div>
</form>
</body>
</html>
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;
using Business;
using OceanSoft.Data;
using EntityControl.Entity;
using System.Data.SqlClient;
public partial class addressbook_show : System.Web.UI.Page
{
private AddressBookDataSet addressbookds = null;
private AddressBookManager addressbookmanager = null;
private AddressBookDataSet.htelDataTable hteldt = null;
private int widestData;
protected void Page_Load(object sender, EventArgs e)
{
//首次调用页面,下次不进行Post提交,作为客户端组件运行
if (!IsPostBack)
{
this.ViewState["btnisclick"] = "false";
this.Bind("");
}
}
/// <summary>
/// 显示分页
/// </summary>
private void showPage()
{
LblCurrentIndex.Text = "第 " + (this.gvhtel.PageIndex + 1).ToString() + " 页";
LblPageCount.Text = "共 " + this.gvhtel.PageCount.ToString() + " 页";
LblRecordCount.Text = "总共 " + hteldt.Rows.Count.ToString() + " 条";
if (hteldt.Rows.Count == 0)
{
btnFirst.Visible = false;
btnPrev.Visible = false;
btnNext.Visible = false;
btnLast.Visible = false;
LblCurrentIndex.Visible = false;
LblPageCount.Visible = false;
LblRecordCount.Visible = false;
}
else if (this.gvhtel.PageCount == 1)
{
btnFirst.Visible = false;
btnPrev.Visible = false;
btnNext.Visible = false;
btnLast.Visible = false;
}
// 计算生成分页页码,分别为:"首 页" "上一页" "下一页" "尾 页"
btnFirst.CommandName = "1";
btnPrev.CommandName = (this.gvhtel.PageIndex == 0 ? "1" : this.gvhtel.PageIndex.ToString());
btnNext.CommandName = (this.gvhtel.PageCount == 1 ? this.gvhtel.PageCount.ToString() : (this.gvhtel.PageIndex + 2).ToString());
btnLast.CommandName = this.gvhtel.PageCount.ToString();
//
this.ddlCurrentPage.Items.Clear();
for (int i = 1; i <= this.gvhtel.PageCount; i++)
{
this.ddlCurrentPage.Items.Add(i.ToString());
}
this.ddlCurrentPage.SelectedIndex = this.gvhtel.PageIndex;
}
/// <summary>
/// 数据集绑定
/// </summary>
/// <param name="s"></param>
private void Bind(string s)
{
addressbookds = new AddressBookDataSet();
addressbookmanager = new AddressBookManager();
hteldt = addressbookmanager.Search(s);
try
{
this.gvhtel.DataSource = hteldt.DefaultView;
this.gvhtel.DataBind();
this.showPage();
}
catch (Exception e)
{
Response.Write(e.Message);
}
}
/// <summary>
/// 判断按纽是否被按下并绑定数据集
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Judge()
{
if (this.ViewState["btnisclick"].ToString() == "true")
{
this.Bind(this.txtSearch.Text.Trim());
}
else
{
this.Bind("");
}
}
protected void btnSearch_Click(object sender, EventArgs e)
{
this.ViewState["btnisclick"] = "true";
this.Bind(this.txtSearch.Text.Trim());
}
protected void PagerButtonClick(object sender, EventArgs e)
{
this.gvhtel.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandName) - 1;
this.Judge();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
this.gvhtel.PageIndex = this.ddlCurrentPage.SelectedIndex;
this.Judge();
}
protected void gvhtel_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Attributes["onmouseover"] = "ItemOver(this)";
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="show.aspx.cs" Inherits="addressbook_show" Debug="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
<html xmlns="
<head runat="server">
<title>内部黄页</title>
<link rel="stylesheet" rev="stylesheet" href="../css/style.css" type="text/css" media="all"/>
<script language="javascript" type="text/javascript">
// 鼠标经过改变行的颜色
if (!objbeforeItem)
{
var objbeforeItem=null;
var objbeforeItembackgroundColor=null;
}
function ItemOver(obj)
{
if(objbeforeItem)
{
objbeforeItem.style.backgroundColor = objbeforeItembackgroundColor;
}
objbeforeItembackgroundColor = obj.style.backgroundColor;
objbeforeItem = obj;
obj.style.backgroundColor = "#B9D1F3";
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<center><asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" OnClick="btnSearch_Click" Text="查 询" class=btn3_mouseout onmouseover="this.className='btn3_mouseover'"
onmouseout="this.className='btn3_mouseout'" onmousedown="this.className='btn3_mousedown'" onmouseup="this.className='btn3_mouseup'" style="width: 60px"/></center><br>
<table>
<tr><td>
<asp:GridView ID="gvhtel" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" AllowPaging="true" PageSize="20" PagerSettings-Visible="false" OnRowDataBound="gvhtel_RowDataBound">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:BoundField DataField="code" HeaderText="工号" HeaderStyle-Width="50px"/>
<asp:BoundField DataField="name" HeaderText="姓名" HeaderStyle-Width="100px"/>
<asp:BoundField DataField="department" HeaderText="科室" HeaderStyle-Width="100px"/>
<asp:BoundField DataField="tel" HeaderText="宅电" HeaderStyle-Width="70px"/>
<asp:BoundField DataField="mtel" HeaderText="手机" HeaderStyle-Width="70px"/>
<asp:BoundField DataField="xlt" HeaderText="小灵通" HeaderStyle-Width="70px"/>
<asp:BoundField DataField="htel" HeaderText="院内电话" HeaderStyle-Width="70px"/>
<asp:BoundField DataField="floor" HeaderText="楼层" HeaderStyle-Width="50px"/>
<asp:BoundField DataField="hmail" HeaderText="院内邮件" HeaderStyle-Width="80px"/>
<asp:BoundField DataField="mail" HeaderText="MSN" HeaderStyle-Width="50px"/>
<asp:BoundField DataField="qq" HeaderText="QQ" HeaderStyle-Width="50px"/>
</Columns>
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<EditRowStyle BackColor="#999999" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
</td></tr>
<tr><td>
<asp:LinkButton ID="btnFirst" CommandArgument="first" OnClick="PagerButtonClick"
runat="server">首 页</asp:LinkButton> <asp:LinkButton ID="btnPrev" CommandArgument="prev" OnClick="PagerButtonClick" runat="server">上一页</asp:LinkButton>
<asp:LinkButton ID="btnNext" CommandArgument="next" OnClick="PagerButtonClick" runat="server">下一页</asp:LinkButton>
<asp:LinkButton ID="btnLast" CommandArgument="last" OnClick="PagerButtonClick" runat="server">尾 页</asp:LinkButton>
<asp:Label ID="LblCurrentIndex" runat="server"></asp:Label>
<asp:Label ID="LblPageCount" runat="server"></asp:Label>
<asp:Label ID="LblRecordCount" runat="server"></asp:Label>
跳转到第<asp:DropDownList ID="ddlCurrentPage" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>页
</td></tr>
</table>
</div>
</form>
</body>
</html>
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;
using Business;
using OceanSoft.Data;
using EntityControl.Entity;
using System.Data.SqlClient;
public partial class addressbook_show : System.Web.UI.Page
{
private AddressBookDataSet addressbookds = null;
private AddressBookManager addressbookmanager = null;
private AddressBookDataSet.htelDataTable hteldt = null;
private int widestData;
protected void Page_Load(object sender, EventArgs e)
{
//首次调用页面,下次不进行Post提交,作为客户端组件运行
if (!IsPostBack)
{
this.ViewState["btnisclick"] = "false";
this.Bind("");
}
}
/// <summary>
/// 显示分页
/// </summary>
private void showPage()
{
LblCurrentIndex.Text = "第 " + (this.gvhtel.PageIndex + 1).ToString() + " 页";
LblPageCount.Text = "共 " + this.gvhtel.PageCount.ToString() + " 页";
LblRecordCount.Text = "总共 " + hteldt.Rows.Count.ToString() + " 条";
if (hteldt.Rows.Count == 0)
{
btnFirst.Visible = false;
btnPrev.Visible = false;
btnNext.Visible = false;
btnLast.Visible = false;
LblCurrentIndex.Visible = false;
LblPageCount.Visible = false;
LblRecordCount.Visible = false;
}
else if (this.gvhtel.PageCount == 1)
{
btnFirst.Visible = false;
btnPrev.Visible = false;
btnNext.Visible = false;
btnLast.Visible = false;
}
// 计算生成分页页码,分别为:"首 页" "上一页" "下一页" "尾 页"
btnFirst.CommandName = "1";
btnPrev.CommandName = (this.gvhtel.PageIndex == 0 ? "1" : this.gvhtel.PageIndex.ToString());
btnNext.CommandName = (this.gvhtel.PageCount == 1 ? this.gvhtel.PageCount.ToString() : (this.gvhtel.PageIndex + 2).ToString());
btnLast.CommandName = this.gvhtel.PageCount.ToString();
//
this.ddlCurrentPage.Items.Clear();
for (int i = 1; i <= this.gvhtel.PageCount; i++)
{
this.ddlCurrentPage.Items.Add(i.ToString());
}
this.ddlCurrentPage.SelectedIndex = this.gvhtel.PageIndex;
}
/// <summary>
/// 数据集绑定
/// </summary>
/// <param name="s"></param>
private void Bind(string s)
{
addressbookds = new AddressBookDataSet();
addressbookmanager = new AddressBookManager();
hteldt = addressbookmanager.Search(s);
try
{
this.gvhtel.DataSource = hteldt.DefaultView;
this.gvhtel.DataBind();
this.showPage();
}
catch (Exception e)
{
Response.Write(e.Message);
}
}
/// <summary>
/// 判断按纽是否被按下并绑定数据集
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Judge()
{
if (this.ViewState["btnisclick"].ToString() == "true")
{
this.Bind(this.txtSearch.Text.Trim());
}
else
{
this.Bind("");
}
}
protected void btnSearch_Click(object sender, EventArgs e)
{
this.ViewState["btnisclick"] = "true";
this.Bind(this.txtSearch.Text.Trim());
}
protected void PagerButtonClick(object sender, EventArgs e)
{
this.gvhtel.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandName) - 1;
this.Judge();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
this.gvhtel.PageIndex = this.ddlCurrentPage.SelectedIndex;
this.Judge();
}
protected void gvhtel_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Attributes["onmouseover"] = "ItemOver(this)";
}
}