有没有简单的分页啊? 我上网查了C#的分页代码也是非常复杂啊,请问有简单的分页吗?在下是个C#菜鸟,希望各位前辈多教教我! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在网上找个存储过程分页然后再找一个 aspnetpage 就行了 要简单的那就用自带的比如gridview就自带分页就一个事件而已 public static IList<ListLoan> GetAllListLoansnotUID(int page) { int rowBegin = 0; // 开始行数,表示每页第一条记录在数据库中的行数 if (page > 1) { rowBegin = 7 * (page - 1); // 按页数取得开始行数,设每页可以显示10条回复 } string sqlAll = "select top 7 * from ListLoan where UID <> 0 and ID not in(select top " + rowBegin + " ID from ListLoan where UID <> 0 order by Posttime )order by Posttime"; return GetListLoansBySql(sqlAll); } private static IList<ListLoan> GetListLoansBySql(string safeSql) { List<ListLoan> list = new List<ListLoan>(); try { DataTable table = DBHelper.GetDataSet(safeSql); foreach (DataRow row in table.Rows) { ListLoan listLoan = new ListLoan(); listLoan.ID = (int)row["ID"]; listLoan.Loantype = (string)row["Loantype"]; listLoan.Purpose = (string)row["Purpose"]; listLoan.Timelimit = (string)row["Timelimit"]; listLoan.DistrictProvince = (string)row["DistrictProvince"]; listLoan.DistrictCity = (string)row["DistrictCity"]; listLoan.Money = (decimal)row["Money"]; listLoan.Interestrate = (decimal)row["Interestrate"]; listLoan.Title = (string)row["Title"]; listLoan.Explain = (string)row["Explain"]; listLoan.Lastname = (string)row["Lastname"]; listLoan.Phone = (string)row["Phone"]; listLoan.Cellphone = (string)row["Cellphone"]; listLoan.IdentityCard = (string)row["IdentityCard"]; listLoan.Email = (string)row["Email"]; listLoan.UID = (int)row["UID"]; listLoan.Pledge = (string)row["Pledge"]; listLoan.Posttime = (DateTime)row["Posttime"]; listLoan.Condition = (int)row["Condition"]; list.Add(listLoan); } return list; } catch (Exception e) { Console.WriteLine(e.Message); throw e; } } GridView有自带的分页功能啊.......你看下他的事件里面有GridVIew_SelectIndexChange的事件吧.这个事件就是分页的..就添一行代码就可以了.GridView1.PageIndex=e; protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) { userinfo user = new userinfo(); GridView1.DataSource = user.DaSet(); GridView1.DataBind(); this.myFillDDL(); this.myDB(Convert.ToInt32(this.DropDownList1.SelectedItem.Text) - 1); } } private void myDB(int currentPage) { // DAL dal =new DAL(); SqlConnection myCnn = new SqlConnection(@"server=;uid=;pwd=;database="); SqlDataAdapter DA = new SqlDataAdapter("select * from contents", myCnn); DataSet DS = new DataSet(); DA.Fill(DS, currentPage * 5, 5, "a"); this.GridView1.DataSource = DS.Tables["a"].DefaultView; this.GridView1.DataBind(); } private void myFillDDL() { //DAL dal = new DAL(); SqlConnection myCnn = new SqlConnection(@"server=;uid=;pwd=;database="); SqlCommand myCmd = new SqlCommand("select count(*) from contents", myCnn); myCnn.Open(); int total = Convert.ToInt32(myCmd.ExecuteScalar()); myCnn.Close(); int pages = (total + 5 - 1) / 5; for (int i = 1; i <= pages; i++) this.DropDownList1.Items.Add(i.ToString()); } 修改修改能直接用,我也是菜鸟,一起学习啊 使用AspNetPager分页控件,简单,方便,设置一下就可以了 你想要有多简单用分页控件 只要用sql把 两个参数 传给他就行了 AspNetPager 一直用 很简单 谢谢各位大哥的帮忙,我用DataGrid分页了,很简单,就一行代码。 这个我一直用的,挺简单 int intStartPage = 1;//要显示的第一页,如显示56789,则为5 int intEndPage = intPageNum;//intPageNum为总页数,要显示的最后页,如显示56789,则为9 int intActivePage = int.Parse(this.page);// 当前页 if (intActivePage > intPageNum) { intActivePage = intPageNum; } //以下用于计算出intStartPage 和intEndPage if (intEndPage - intStartPage > intMaxPageToDisplay) { int intHalfOfMaxPages = (intMaxPageToDisplay / 2); if (intActivePage > 2) { intStartPage = intActivePage - intHalfOfMaxPages; if (intStartPage < 1) { intStartPage = 1; } } intEndPage = intActivePage + intHalfOfMaxPages; if (intStartPage == 1) { intEndPage += (intHalfOfMaxPages - intActivePage + 1); } if (intEndPage > intPageNum) { intEndPage = intPageNum; } if (intEndPage == intPageNum) { intStartPage = intEndPage - intMaxPageToDisplay; } if (intStartPage < 1) { intStartPage = 1; } } 一贯datalist。。分页控件aspnetpage或网上荡的手写版 都很方便 分页控件蛮好用的 比如:aspnetpage private void Databind() { //对PagedDataSource 对象的相关属性赋值 PagedDataSource pdsAdvice = new PagedDataSource(); //获得同一类的信息集合 pdsAdvice.DataSource = 绑定数据源 //启动分页 pdsAdvice.AllowPaging = true; //每页显示几条信息 pdsAdvice.PageSize = 10; //获得当前页数的索引 pdsAdvice.CurrentPageIndex = Pager; this.lblPage.Text = "当前页:" + (pdsAdvice.CurrentPageIndex + 1).ToString(); //调用控件的显示方法 SetEnable(pdsAdvice); this.GridView1.DataSourceID = null; //获取数据源(绑定数据控件ID:如GridView Repeater datalist等) this.绑定数据控件ID.DataSource = pdsAdvice; //绑定数据源 this.绑定数据控件ID.DataBind(); } //上一页 protected void btnPrev_Click(object sender, EventArgs e) { //当前页数减1 Pager--; Databind(); } //下一页 protected void btnNext_Click(object sender, EventArgs e) { //当前页数加一 Pager++; Databind(); } /// <summary> /// 当前页数 /// </summary> private int Pager { get { return (int)ViewState["Page"]; } set { ViewState["Page"] = value; } } //是否显示 private void SetEnable(PagedDataSource pds) { //显示上一页 btnPrev.Enabled = true; //显示下一页 btnNext.Enabled = true; //当首页时,上一页不显示 if (pds.IsFirstPage) btnPrev.Enabled = false; //当尾页时,上一页不显示 if (pds.IsLastPage) btnNext.Enabled = false; } <table width="100%"> <tr> <td style="text-align:right"> 第<asp:Label id="lblPageIndex" runat="server" text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>' />页 共/<asp:Label id="lblPageCount" runat="server" text='<%# ((GridView)Container.Parent.Parent).PageCount %>' />页 <asp:linkbutton id="btnFirst" runat="server" causesvalidation="False" commandargument="First" commandname="Page" text="首页" /> <asp:linkbutton id="btnPrev" runat="server" causesvalidation="False" commandargument="Prev" commandname="Page" text="上一页" /> <asp:linkbutton id="btnNext" runat="server" causesvalidation="False" commandargument="Next" commandname="Page" text="下一页" /> <asp:linkbutton id="btnLast" runat="server" causesvalidation="False" commandargument="Last" commandname="Page" text="尾页" /> <asp:textbox id="txtNewPageIndex" runat="server" width="20px" text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>' /> <asp:linkbutton id="btnGo" runat="server" causesvalidation="False" commandargument="-1" commandname="Page" text="GO" /><!-- here set the CommandArgument of the Go Button to '-1' as the flag --> </td> </tr> </table> 插入到分页模板中 网页部分 <div style="text-align: center; font-size: 12px;"> <asp:Label ID="lblPage" runat="server" Text="lblText"></asp:Label> <asp:LinkButton ID="btnPrev" runat="server" ForeColor="Blue" OnClick="btnPrev_Click">上一页</asp:LinkButton> <asp:LinkButton ID="btnNext" runat="server" ForeColor="Blue" OnClick="btnNext_Click">下一页</asp:LinkButton> </div>自己在cs文件中调用方法 protected void Page_Load(object sender, EventArgs e) {//当前第几页 ViewState["Page"] = 0; Databind();} GridView有自带的分页功能 设置属性就行了 DataGrid 自带分页,写下<pageTemplate>可以自定义样式 伪静态不生效 求一正则表达式 sql2005和sql2000中的临时表问题???? frame局部刷新 再发 求一条sql查询语句 详细说明 sqldatareader排相片 asp 用Jmail发邮件??? 请大家帮忙看看 哪位大哥有ASP.NET的源码,有关学生信息管理的 如何在一个自定义类里实现消息提示 SQL语句 求一个非常非常早的OA
比如gridview就自带分页
就一个事件而已
{
int rowBegin = 0; // 开始行数,表示每页第一条记录在数据库中的行数
if (page > 1)
{ rowBegin = 7 * (page - 1); // 按页数取得开始行数,设每页可以显示10条回复
}
string sqlAll = "select top 7 * from ListLoan where UID <> 0 and ID not in(select top " + rowBegin + " ID from ListLoan where UID <> 0 order by Posttime )order by Posttime";
return GetListLoansBySql(sqlAll);
}
private static IList<ListLoan> GetListLoansBySql(string safeSql)
{
List<ListLoan> list = new List<ListLoan>(); try
{
DataTable table = DBHelper.GetDataSet(safeSql); foreach (DataRow row in table.Rows)
{
ListLoan listLoan = new ListLoan(); listLoan.ID = (int)row["ID"];
listLoan.Loantype = (string)row["Loantype"];
listLoan.Purpose = (string)row["Purpose"];
listLoan.Timelimit = (string)row["Timelimit"];
listLoan.DistrictProvince = (string)row["DistrictProvince"];
listLoan.DistrictCity = (string)row["DistrictCity"];
listLoan.Money = (decimal)row["Money"];
listLoan.Interestrate = (decimal)row["Interestrate"];
listLoan.Title = (string)row["Title"];
listLoan.Explain = (string)row["Explain"];
listLoan.Lastname = (string)row["Lastname"];
listLoan.Phone = (string)row["Phone"];
listLoan.Cellphone = (string)row["Cellphone"];
listLoan.IdentityCard = (string)row["IdentityCard"];
listLoan.Email = (string)row["Email"];
listLoan.UID = (int)row["UID"];
listLoan.Pledge = (string)row["Pledge"];
listLoan.Posttime = (DateTime)row["Posttime"];
listLoan.Condition = (int)row["Condition"]; list.Add(listLoan);
} return list;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw e;
} }
这个事件就是分页的..就添一行代码就可以了.
GridView1.PageIndex=e;
{
if(!IsPostBack)
{
userinfo user = new userinfo();
GridView1.DataSource = user.DaSet();
GridView1.DataBind();
this.myFillDDL();
this.myDB(Convert.ToInt32(this.DropDownList1.SelectedItem.Text) - 1);
}
}
private void myDB(int currentPage)
{
// DAL dal =new DAL();
SqlConnection myCnn = new SqlConnection(@"server=;uid=;pwd=;database=");
SqlDataAdapter DA = new SqlDataAdapter("select * from contents", myCnn);
DataSet DS = new DataSet();
DA.Fill(DS, currentPage * 5, 5, "a");
this.GridView1.DataSource = DS.Tables["a"].DefaultView;
this.GridView1.DataBind();
} private void myFillDDL()
{
//DAL dal = new DAL();
SqlConnection myCnn = new SqlConnection(@"server=;uid=;pwd=;database=");
SqlCommand myCmd = new SqlCommand("select count(*) from contents", myCnn);
myCnn.Open();
int total = Convert.ToInt32(myCmd.ExecuteScalar());
myCnn.Close();
int pages = (total + 5 - 1) / 5;
for (int i = 1; i <= pages; i++)
this.DropDownList1.Items.Add(i.ToString());
}
修改修改能直接用,我也是菜鸟,一起学习啊
int intEndPage = intPageNum;//intPageNum为总页数,要显示的最后页,如显示56789,则为9 int intActivePage = int.Parse(this.page);// 当前页
if (intActivePage > intPageNum)
{
intActivePage = intPageNum;
}
//以下用于计算出intStartPage 和intEndPage
if (intEndPage - intStartPage > intMaxPageToDisplay)
{
int intHalfOfMaxPages = (intMaxPageToDisplay / 2);
if (intActivePage > 2)
{
intStartPage = intActivePage - intHalfOfMaxPages;
if (intStartPage < 1)
{
intStartPage = 1;
}
}
intEndPage = intActivePage + intHalfOfMaxPages;
if (intStartPage == 1)
{
intEndPage += (intHalfOfMaxPages - intActivePage + 1);
}
if (intEndPage > intPageNum)
{
intEndPage = intPageNum;
}
if (intEndPage == intPageNum)
{
intStartPage = intEndPage - intMaxPageToDisplay;
}
if (intStartPage < 1)
{
intStartPage = 1;
}
}
分页控件aspnetpage
或网上荡的手写版 都很方便
{
//对PagedDataSource 对象的相关属性赋值
PagedDataSource pdsAdvice = new PagedDataSource();
//获得同一类的信息集合
pdsAdvice.DataSource = 绑定数据源
//启动分页
pdsAdvice.AllowPaging = true;
//每页显示几条信息
pdsAdvice.PageSize = 10;
//获得当前页数的索引
pdsAdvice.CurrentPageIndex = Pager;
this.lblPage.Text = "当前页:" + (pdsAdvice.CurrentPageIndex + 1).ToString();
//调用控件的显示方法
SetEnable(pdsAdvice);
this.GridView1.DataSourceID = null;
//获取数据源(绑定数据控件ID:如GridView Repeater datalist等)
this.绑定数据控件ID.DataSource = pdsAdvice;
//绑定数据源
this.绑定数据控件ID.DataBind();
} //上一页
protected void btnPrev_Click(object sender, EventArgs e)
{
//当前页数减1
Pager--;
Databind();
}
//下一页
protected void btnNext_Click(object sender, EventArgs e)
{
//当前页数加一
Pager++;
Databind();
}
/// <summary>
/// 当前页数
/// </summary>
private int Pager
{
get
{
return (int)ViewState["Page"];
}
set
{
ViewState["Page"] = value;
}
}
//是否显示
private void SetEnable(PagedDataSource pds)
{
//显示上一页
btnPrev.Enabled = true;
//显示下一页
btnNext.Enabled = true;
//当首页时,上一页不显示
if (pds.IsFirstPage)
btnPrev.Enabled = false;
//当尾页时,上一页不显示
if (pds.IsLastPage)
btnNext.Enabled = false;
}
<table width="100%">
<tr>
<td style="text-align:right">
第<asp:Label id="lblPageIndex" runat="server" text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>' />页
共/<asp:Label id="lblPageCount" runat="server" text='<%# ((GridView)Container.Parent.Parent).PageCount %>' />页
<asp:linkbutton id="btnFirst" runat="server" causesvalidation="False" commandargument="First" commandname="Page" text="首页" />
<asp:linkbutton id="btnPrev" runat="server" causesvalidation="False" commandargument="Prev" commandname="Page" text="上一页" />
<asp:linkbutton id="btnNext" runat="server" causesvalidation="False" commandargument="Next" commandname="Page" text="下一页" />
<asp:linkbutton id="btnLast" runat="server" causesvalidation="False" commandargument="Last" commandname="Page" text="尾页" />
<asp:textbox id="txtNewPageIndex" runat="server" width="20px" text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>' />
<asp:linkbutton id="btnGo" runat="server" causesvalidation="False" commandargument="-1" commandname="Page" text="GO" /><!-- here set the CommandArgument of the Go Button to '-1' as the flag -->
</td>
</tr>
</table> 插入到分页模板中
<div style="text-align: center; font-size: 12px;">
<asp:Label ID="lblPage" runat="server" Text="lblText"></asp:Label>
<asp:LinkButton ID="btnPrev" runat="server" ForeColor="Blue" OnClick="btnPrev_Click">上一页</asp:LinkButton>
<asp:LinkButton ID="btnNext" runat="server" ForeColor="Blue" OnClick="btnNext_Click">下一页</asp:LinkButton>
</div>
自己在cs文件中调用方法
protected void Page_Load(object sender, EventArgs e)
{
//当前第几页
ViewState["Page"] = 0;
Databind();
}