连接的数据库无法翻页,请各位大虾帮帮,

aaarockmanexe 2010-11-11 08:01:58
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
...全文
197 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
dingxueshu 2010-11-13
  • 打赏
  • 举报
回复
LZ 分页 有点复杂哦!
Q333111555 2010-11-13
  • 打赏
  • 举报
回复
断点,然后看你的页数设置是不是有问题
myhope88 2010-11-12
  • 打赏
  • 举报
回复
datagrid本身就有自带分页功能的啊,出现这样的错误,一般都是你的分页逻辑上出了问题,你仔细调试下应该就能找出问题了
若-相惜 2010-11-12
  • 打赏
  • 举报
回复
lz一个分页功能没必要那么复杂。。。。。。。。。

用aspnetpager吧


/// <summary>
/// 取得总数
/// </summary>
/// <returns></returns>
public string getTotal()
{
StringBuilder sb = new StringBuilder();
sb.Append("select count(*) total from Test");
DataTable dt = DBHelper.ExecuteDt(sb.ToString());
return dt.Rows[0][0].ToString();
}
/// <summary>
/// 根据当前页码,每页条数,取得相应数据。
/// </summary>
/// <param name="pageNum">每页显示条数</param>
/// <param name="currentPage">当前页码</param>
/// <returns></returns>
public DataTable getPagesData(int pageNum, int currentPage)
{
StringBuilder sb = new StringBuilder();
sb.Append("select top " + pageNum + " * from Test where ");
sb.Append("ID not in (select top " + pageNum * currentPage + " ID from Test)");
return DBHelper.ExecuteDt(sb.ToString());
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="sqlPager_Default" %>
<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>
<!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:gridview ID="gvSql" runat="server">
</asp:gridview>
</div>
<div>
<webdiyer:aspnetpager ID="AspNetPager1" runat="server" OnPageChanged="AspNetPager1_PageChanged" PageSize="3">
</webdiyer:aspnetpager>
</div>
</form>
</body>
</html>

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 sqlPager_Default : System.Web.UI.Page
{
BLL.Test test = new BLL.Test();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
AspNetPager1.RecordCount = Convert.ToInt32(test.getTotal());//此属性保存总记录数..
Bind();
}
}
private void Bind()
{
this.gvSql.DataSource = test.getPagesData(Convert.ToInt32(AspNetPager1.PageSize), AspNetPager1.CurrentPageIndex - 1);
this.gvSql.DataBind();
}
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
Bind();
}
}

latin55 2010-11-12
  • 打赏
  • 举报
回复
是的,要加个判断,你的那个-- 当然超出索引界限啊

建议哈 LZ写代码能规范点不 看得眼花!
michael23lb 2010-11-12
  • 打赏
  • 举报
回复
加入判断 if (myGrid.CurrentPageIndex > 0)
Fry_cici 2010-11-12
  • 打赏
  • 举报
回复
自己写一个分页控件!!!
zhaomeng1230 2010-11-12
  • 打赏
  • 举报
回复
帮顶啊 曾经也遇到过这种情况啊
ToMakeLove 2010-11-12
  • 打赏
  • 举报
回复
控件自带翻页功能,或是手写代码记录翻页的页数咯。把代码写在控件触发事件里哦。
shichao102471077 2010-11-12
  • 打赏
  • 举报
回复
为什么要这样搞额。。用aspbagedatasoure
rwdong2199 2010-11-12
  • 打赏
  • 举报
回复
眼花了...
nitaiyoucala 2010-11-11
  • 打赏
  • 举报
回复
索引问题 建议用aspnetpager
内容概要:本文围绕基于PI双闭环解耦控制的三相电压型PWM整流器在第四象限运行的仿真研究展开,重点分析其在电流反向流动工况下的控制性能。通过Simulink搭建系统模型,采用电压外环与电流内环构成的双闭环PI控制策略,并引入d-q轴解耦环节以消除交叉耦合影响,实现对整流器在能量回馈状态下的高精度、稳定控制。研究涵盖了系统数学建模、控制器参数设计、解耦算法实现及动态响应仿真验证,充分展示了该控制方法在抑制扰动、提升系统鲁棒性方面的有效性。; 适合人群:电力电子、电气工程及其自动化等相关专业的研究生、科研人员及从事新能源变流器、电能质量治理或工业传动系统开发的工程技术人员;具备自动控制理论基础和Simulink仿真能力者更佳。; 使用场景及目标:①深入掌握三相电压型PWM整流器的工作原理及其在不同运行象限的能量流动特性;②理解并实践PI双闭环控制系统的设计思路与参数整定方法;③学习d-q坐标系下电流解耦控制的实现机制;④熟练运用Simulink进行电力电子系统建模与仿真分析;⑤为实际工程中实现高效能量双向变换提供理论依据与技术参考。; 阅读建议:建议结合Simulink环境同步搭建模型,细致分析各模块的信号流向与控制逻辑,重点关注电流内环的动态跟踪能力和电压外环的稳态调节性能,可通过改变负载突变、电网电压波动等条件进行对比实验,进一步评估系统的抗干扰能力与稳定性表现。

62,271

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧