使用DirectoryInfo绑定数据,然后分页的问题。

azhou88 2016-03-31 12:38:36
代码如下,绑定后,如果一次性显示,没有问题,但是如果我把pagesize=2,那么显示就有问题了。每次都只是显示同样的图片。为什么?麻烦通俗直观的解释。谢谢



<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>

<!DOCTYPE html>

<script runat="server">
void page_load()
{
if(!Page.IsPostBack)
{
DirectoryInfo file = new DirectoryInfo(MapPath("~/14/images"));
ListView1.DataSource = file.GetFiles("*.jpg");
ListView1.DataBind();



}
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ListView ID="ListView1" runat="server">
<LayoutTemplate>
<ol>
<asp:PlaceHolder
ID="itemPlaceholder"
runat="server"
/>


</ol>
<asp:DataPager
ID="aa"
PageSize="10"
runat="server"
>
<Fields>
<asp:NextPreviousPagerField
ShowFirstPageButton="true"
ShowPreviousPageButton="true"
ShowNextPageButton="false"
ShowLastPageButton="false" />
<asp:NumericPagerField />
<asp:NextPreviousPagerField
ShowFirstPagebutton="false"
ShowPreviousPageButton="false"
ShowNextPageButton="true"
ShowLastPageButton="true" />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>

<asp:Image
runat="server"
ID="img1"
ImageUrl='<%# Eval("Name","~/14/images/{0}") %>'
Width="500px"
Height="300px"
name="imgA" />
</ItemTemplate>

</asp:ListView>
</div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</form>
</body>
</html>
...全文
159 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
如果不需要重新绑定数据源(数据全都保存在ViewState中了),那么可能就是你的 DataPager 控件使用了。这可能还要看看 DataPager 的入门教程上面的细节。
  • 打赏
  • 举报
回复
(我对asp.net3.5的组件没有什么印象,只记得2.0的)你可以看看 ListView 控件的相关文档,看看其翻页时是否需要重新绑定数据源。
azhou88 2016-03-31
  • 打赏
  • 举报
回复
<%@ Page Language="C#" %> <%@ Import Namespace="System.IO" %> <!DOCTYPE html> <script runat="server"> void page_load() { if (!Page.IsPostBack) { databind(); } } public void databind() { DirectoryInfo file = new DirectoryInfo(MapPath("~/14/images")); ListView1.DataSource = file.GetFiles("*.jpg"); ListView1.DataBind(); } protected void pg_PagerCommand(object sender, DataPagerCommandEventArgs e) { databind(); e.NewMaximumRows = e.Item.Pager.MaximumRows; switch (e.CommandName) { case "Previous": if (e.Item.Pager.StartRowIndex > 0) e.NewStartRowIndex = e.Item.Pager.StartRowIndex - 2; break; case "Next": e.NewStartRowIndex = e.Item.Pager.StartRowIndex + 2; break; } } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:ListView ID="ListView1" runat="server"> <LayoutTemplate> <ol> <asp:PlaceHolder ID="itemPlaceholder" runat="server" /> </ol> <asp:DataPager id="pg" PageSize="2" Runat="server"> <Fields> <asp:TemplatePagerField OnPagerCommand="pg_PagerCommand"> <PagerTemplate> <asp:LinkButton id="lnkPrevious" Text="Previous" CommandName="Previous" Runat="server" /> <asp:LinkButton id="lnkNext" Text="Next" CommandName="Next" Runat="server" /> </PagerTemplate> </asp:TemplatePagerField> </Fields> </asp:DataPager> </LayoutTemplate> <ItemTemplate> <li> <asp:Image runat="server" ID="img1" ImageUrl='<%# Eval("Name","~/14/images/{0}") %>' Width="500px" Height="300px" name="imgA" /> </li> </ItemTemplate> </asp:ListView> </div> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </form> </body> </html> 这样子好点。但是当点击上一页的时候还是不行啊。
azhou88 2016-03-31
  • 打赏
  • 举报
回复
谢谢你。但是找详细的,这些方面的文章好难啊。

62,243

社区成员

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

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

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

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