using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
namespace phone
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid myGrid;
int n;
protected System.Web.UI.WebControls.TextBox txtKey;
protected System.Web.UI.WebControls.Button ButtonDown;
protected System.Web.UI.WebControls.Button ButtonUp;
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Web.UI.WebControls.Label LabelRecordCount;
protected System.Web.UI.WebControls.Label LabelPageCount;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.Button Button3;//记录数变量
int m;//页数变量
private void Page_Load(object sender, System.EventArgs e)
{
showAll();
showbutton();// 在此处放置用户代码以初始化页面
}void showAll()
{
//新建连接对象
SqlConnection conn = new SqlConnection();
//从配置文件中获取信息
conn.ConnectionString =
ConfigurationSettings.AppSettings["ConnectionString"];
//新建命令对象
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Select ID,pName,pdata,pmoney,prercel,plook from phone";
cmd.Connection = conn;
//新建数据适配器对象
SqlDataAdapter da = new SqlDataAdapter();
//指定数据适配器对象的Select属性
da.SelectCommand = cmd;
//新建DataSet对象
DataSet result = new DataSet();
//用数据适配器填充DataSet
n=da.Fill(result,"phone");
//绑定数据到DataGrid
this.myGrid.DataSource = result;
this.myGrid.DataBind();
m=myGrid.PageCount;
}
void Search()
{
//获取搜索关键字
string key = this.txtKey.Text.Trim();
//关键字不空时执行搜索
if(key != "")
{
//新建连接
SqlConnection conn = new SqlConnection();
conn.ConnectionString =
ConfigurationSettings.AppSettings["ConnectionString"];
//新建命令
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Select ID,pName,pdata,pmoney,prercel,plook from phone ";
//按姓名搜索
cmd.CommandText += "where pName like '%" + key + "%' ";
//按地址搜索
cmd.CommandText += "or pdata like '%" + key + "%' ";
//按电话搜索
cmd.CommandText += "or pmoney like '%" + key + "%' ";
//按邮箱搜索
//按邮箱搜索cmd.CommandText += "or prercel like '%" + key + "%' ";
//按邮箱搜索
cmd.CommandText += "or plook like '%" + key + "%' ";
cmd.Connection = conn;
//新建数据适配器对象
SqlDataAdapter da = new SqlDataAdapter();
//指定数据适配器对象的Select属性
da.SelectCommand = cmd;
//新建DataSet对象
DataSet result = new DataSet();
//用数据适配器填充DataSet
n=da.Fill(result,"phone");
//绑定数据到DataGrid
this.myGrid.DataSource = result;
this.myGrid.DataBind();
m=myGrid.PageCount;
}
else
{
this.showAll();
}
}
void showbutton()
{
if(n!=0)
{
//若有数据记录就使分页导航控件可见
ButtonUp.Visible=true;
ButtonDown.Visible=true;
DropDownList1.Visible=true;
Label2.Visible=true;
Label3.Visible=true;
LabelRecordCount.Visible=true;
LabelPageCount.Visible=true;
LabelRecordCount.Text="共"+n.ToString()+"条记录";
LabelPageCount.Text="共"+m.ToString()+"页";
myGrid.CurrentPageIndex=0;
//初始化控件DropDownList1的项目
//项目的第一个值从1开始,它对应的页索引号为0
DropDownList1.Items.Clear();
for(int i=1;i<=m;i++)
DropDownList1.Items.Add(i.ToString());
//在第一页使"上一页"按钮不可用
ButtonUp.Enabled=false;
if(m==1)
{
//若只有一页使"下一页"按钮不可用
ButtonDown.Enabled=false;
}
else
{
//若不止一页使"下一页"按钮可用
ButtonDown.Enabled=true;
}
}
else
{
//若无数据记录就使分页导航控件不可见
ButtonUp.Visible=false;
ButtonDown.Visible=false;
DropDownList1.Visible=false;
Label2.Visible=false;
Label3.Visible=false;
LabelRecordCount.Visible=false;
LabelPageCount.Visible=false;
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.myGrid.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);
this.ButtonUp.Click += new System.EventHandler(this.ButtonUp_Click);
this.ButtonDown.Click += new System.EventHandler(this.ButtonDown_Click);
this.DropDownList1.SelectedIndexChanged += new System.EventHandler(this.DropDownList1_SelectedIndexChanged);
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Button3.Click += new System.EventHandler(this.Button3_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
{
}
private void Button1_Click(object sender, System.EventArgs e)
{
myGrid.CurrentPageIndex=0;
Search();
this.showbutton();
}
private void Button2_Click(object sender, System.EventArgs e)
{
//使当前页的索引号等于0
myGrid.CurrentPageIndex=0;
//显示所有联系人
this.showAll();
this.showbutton();
}
private void Button3_Click(object sender, System.EventArgs e)
{
//重定向到添加联系人页面
Response.Redirect("Add.aspx");
}
private void ButtonUp_Click(object sender, System.EventArgs e)
{//当前页的索引号减1
myGrid.CurrentPageIndex--;
//使DropDownList1控件显示当前页号
//为使读者习惯,看到的页号比当前页的索引号大1
DropDownList1.SelectedIndex=myGrid.CurrentPageIndex;
ButtonDown.Enabled=true;
if(myGrid.CurrentPageIndex==0)
ButtonUp.Enabled=false;
//重新绑定数据
this.Search();
}
private void ButtonDown_Click(object sender, System.EventArgs e)
{
//当前页的索引号加1
myGrid.CurrentPageIndex++;
//使DropDownList1控件显示当前页号
DropDownList1.SelectedIndex=myGrid.CurrentPageIndex;
ButtonUp.Enabled=true;
if(myGrid.CurrentPageIndex==myGrid.PageCount-1)
ButtonDown.Enabled=false;
//重新绑定数据
this.Search();
}
private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
//取DropDownList1控件中用户改变后的索引号
int n=DropDownList1.SelectedIndex;
//使当前页的索引号等于DropDownList1控件的选择项的索引号
myGrid.CurrentPageIndex=n;
if(n==0)
{
ButtonUp.Enabled=false;
ButtonDown.Enabled=true;
}
else
{
if(n==myGrid.PageCount-1)
{
ButtonDown.Enabled=false;
ButtonUp.Enabled=true;
}
else
{
ButtonUp.Enabled=true;
ButtonDown.Enabled=true;
}
}
//重新绑定数据
this.Search();
}
}
图一

图二

代码可能长了些,希望大家帮帮,我研究几天了,快倒下了,数据库连接成功的,只是在翻页上出了问题,只能翻到第二页,到第二页就翻不了(按“下一页”没反映),在第二页按“上一页”就出现图二,晕,根本看不懂啊....请各位大虾帮帮,小弟万分感谢.. 上一页的Button我改为ButtonUp,下一页的Button我改为ButtonUpDown