请教如何用PageDataSource为DataList分页

我左边有个TreeView里面的子节点就是C#,.NET.C++等等那些图书大类。我现在想点C#右边的DataList从数据库读取所有C#这个书类的所有图书并对其查出来显... 我左边有个TreeView里面的子节点就是C#,.NET.C++等等那些图书大类。我现在想点C#右边的DataList从数据库读取所有C# 这个书类的所有图书并对其查出来显示的图书进行分页。显示图书的部分我已经做好啦。就是分页。请问各位大虾。接下来该怎么弄这个分页。最好给个小例子。谢谢 展开
 我来答
一只鱼在天上飞
2010-11-06 · 超过13用户采纳过TA的回答
知道答主
回答量:64
采纳率:0%
帮助的人:30.5万
展开全部
--------DataList分页-------
private void indexListBind()
{
try
{
string parID = Request.QueryString["id"].ToString();
int curPage = Convert.ToInt32(this.lb_pageIndex.Text) - 1;//获取当前页码,我用的是一个lable.当了中间变量
con = Admin_DB.getConn();
con.Open();
DataSet ds = new DataSet();
string Path = "~";
OleDbDataAdapter sda = new OleDbDataAdapter("select ID,'" + Path + "'+imagePath as imagePath,introduce from D_Picture where parID=@parID", con);
sda.SelectCommand.Parameters.Add(new OleDbParameter("@parID", parID));
sda.Fill(ds, "index");
System.Web.UI.WebControls.PagedDataSource ps = new PagedDataSource();//实例化分页数据源
ps.DataSource = ds.Tables["index"].DefaultView;//将要绑定在datalist上的datatable给分页数据源
ps.AllowPaging = true;
ps.PageSize = 8;//每页显示几条记录
ps.CurrentPageIndex = curPage;//设置当前页的索引(当前页码减1就是)
this.btn_up.Enabled = true;
this.btn_next.Enabled = true;
this.btn_frist.Enabled = true;
this.btn_end.Enabled = true;
endPage = ps.PageCount;
if (curPage == 0)//当是第一页是.上一页和首页的按钮不可用
{
this.btn_up.Enabled = false;
this.btn_frist.Enabled = false;
}
if (curPage == ps.PageCount - 1)//当是最后一页时下一页和最后一页的按钮不可用
{
this.btn_next.Enabled = false;
this.btn_end.Enabled = false;
}
this.indexList.DataSource = ps;
this.indexList.DataKeyField = "ID";
this.indexList.DataBind();
con.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}

}
protected void btn_frist_Click(object sender, EventArgs e)
{
this.lb_pageIndex.Text = "1";
this.indexListBind();
}
protected void btn_up_Click(object sender, EventArgs e)
{
int page = int.Parse(this.lb_pageIndex.Text) - 1;
this.lb_pageIndex.Text = page.ToString();
this.indexListBind();
}
protected void btn_next_Click(object sender, EventArgs e)
{
int page = int.Parse(this.lb_pageIndex.Text) + 1;
this.lb_pageIndex.Text = page.ToString();
this.indexListBind();
}
protected void btn_end_Click(object sender, EventArgs e)
{
this.lb_pageIndex.Text = endPage.ToString();
this.indexListBind();
}

来源:http://zhidao.baidu.com/question/104210550.html?fr=ala0
七鑫易维信息技术
2024-09-02 广告
Play Video 七鑫易维是致力于机器视觉和人工智能领域的高新科技企业,迄今已专注眼球追踪技术的研发、创新与应用超过14年,拥有完全自主知识产权,全球专利总量655余项。 作为眼球追踪技术领域的全球知名品牌,七鑫易维的产品体系覆盖眼动分... 点击进入详情页
本回答由七鑫易维信息技术提供
davis16345
2010-11-06 · 超过17用户采纳过TA的回答
知道答主
回答量:133
采纳率:0%
帮助的人:57.8万
展开全部
<table style="width:97%; margin-left:10px; margin-top:10px; font-size:12px;">
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<tr >
<td style=" border-bottom:dashed 1px #FFCC66; padding-bottom:2px;height:24px;">
<img src="images/arrow4.gif" /> <a href="content.aspx?newid=<%#Eval("id") %>" style="color:<%#Convert.ToInt32(Eval("shenhe"))==1?"red":"" %>"> <%#Eval("title") %></a>    评论:(<font color="red"><%#Eval("pinglun")%></font>)  阅读:(<font color="red"><%#Eval("readchi")%></font>)
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
<div style=" margin-left:130px; margin-top:30px; ">
<asp:LinkButton ID="toone" runat="server" OnClick="toone_Click">[首页]</asp:LinkButton>
<asp:LinkButton ID="toup" runat="server" OnClick="toup_Click">[上一页]</asp:LinkButton> 
<asp:LinkButton ID="tonext" runat="server" OnClick="tonext_Click">[下一页]</asp:LinkButton>
<asp:LinkButton ID="tolast" runat="server" OnClick="tolast_Click">[尾页]</asp:LinkButton> 
[页次:<asp:Label ID="Label1" runat="server" ForeColor="red"></asp:Label> / <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>页]
[共<asp:Label ID="pagesum" runat="server" Text="Label" ForeColor="red"></asp:Label>项
<asp:Label ID="tpagesizes" runat="server" Text="Label" ForeColor="red"></asp:Label>项/页] 跳到
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" Font-Size="12px" Height="10px">
</asp:DropDownList>
</div>

后台

static int nowpages;//全局变量,当前页
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
nowpages = 1;
dlbd();
}
}
protected void dlbd()
{
PagedDataSource ps = new PagedDataSource();
ps.DataSource = mydata.CreateDataView("select * from news order by shenhe desc,id desc");
ps.AllowPaging = true;
ps.PageSize =17;
ps.CurrentPageIndex = nowpages - 1;
toone.Enabled = true;
toup.Enabled = true;
tonext.Enabled = true;
tolast.Enabled = true;
if (nowpages == 1)
{
toone.Enabled = false;
toup.Enabled = false;
}
if (nowpages == ps.PageCount)
{
tonext.Enabled = false;
tolast.Enabled = false;
}
Label1.Text = nowpages.ToString();
Label2.Text = ps.PageCount.ToString();
tpagesizes.Text = ps.PageSize.ToString();
if (!IsPostBack)
{
for (int i = 1; i <= ps.PageCount; i++)
{
DropDownList1.Items.Add(Convert.ToString(i));
}
}
pagesum.Text = ps.DataSourceCount.ToString();
Repeater1.DataSource = ps;
//DataList1.DataKeyField = "id";
Repeater1.DataBind();

}
protected void toone_Click(object sender, EventArgs e)
{
nowpages = 1;
dlbd();
}
protected void toup_Click(object sender, EventArgs e)
{
--nowpages;
dlbd();
}
protected void tonext_Click(object sender, EventArgs e)
{
++nowpages;
dlbd();
}
protected void tolast_Click(object sender, EventArgs e)
{
nowpages = Convert.ToInt32(Label2.Text);
dlbd();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
nowpages = Convert.ToInt32(((DropDownList)sender).SelectedValue);
dlbd();
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式