asp listview数据源不更新

时间:2014-11-13 16:38:54

标签: asp.net vb.net listview datapager

如果我运行此项目,请单击数据目录的第3页,然后单击我的"私有"链接,将新列表绑定到列表视图,它不显示任何内容。调试器显示数据源计数= 9是正确的。数据源项目计数也应该是9,但是它的0.这是问题所在,但我无法弄清楚为什么会发生这种情况。有什么想法吗?

WebForm1.aspx.vb

Imports System.IO

Public Class WebForm1
Inherits System.Web.UI.Page

Private publicPath As String = "~/public"
Private privatePath As String = "~/private"
Private physicalPath As String

Private publicImage As New List(Of String)
Private privateImage As New List(Of String)

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1))
    Response.Cache.SetValidUntilExpires(False)
    Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches)
    Response.Cache.SetCacheability(HttpCacheability.NoCache)
    Response.Cache.SetNoStore()

    getObjects()

    If Session("Mode") Is Nothing Then
        Session("Mode") = "Public"
    End If
End Sub


Private Sub Permissions_PreRender(sender As Object, e As System.EventArgs) Handles Me.PreRender
    'Binds the Data Before Rendering
    BindData()
End Sub


Private Sub getObjects()
    For x As Integer = 0 To 99
        publicImage.Add(x.ToString + ".jpg")
    Next

    For y As Integer = 0 To 8
        privateImage.Add(y.ToString + ".jpg")
    Next
End Sub


Private Function GetListOfImages() As List(Of String)
    Dim images = New List(Of String)()

    If Session("Mode") = "Public" Then
        Me.Title = "Public Image Gallery"

        For Each s As String In publicImage
            images.Add(String.Format("{0}/{1}", publicPath, s))
        Next
    ElseIf Session("Mode") = "Private" Then
        Me.Title = "Public Image Gallery"

        For Each s As String In privateImage
            images.Add(String.Format("{0}/{1}", privatePath, s))
        Next
    End If

    Return images
End Function


''' <summary>
''' Binds the ImageListView to current DataSource
''' </summary>
Private Sub BindData()
    ImageListView.DataSource = GetListOfImages()
    ImageListView.DataBind()

    Me.DataPager1.Visible = Me.DataPager1.PageSize < Me.DataPager1.TotalRowCount ' Don't show datapager if it only has one page
End Sub

Protected Sub lbPublic_Click(sender As Object, e As EventArgs) Handles lbPublic.Click
    Session("Mode") = "Public"
End Sub

Protected Sub lbPrivate_Click(sender As Object, e As EventArgs) Handles lbPrivate.Click
    Session("Mode") = "Private"
End Sub


End Class

WebForm1.aspx的

 <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb"  Inherits="datasourcenotupdating.WebForm1" %>

 <!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>
    <link href="StyleSheet1.css" rel="stylesheet" type="text/css" />
 </head>
 <body>     
<form id="form1" runat="server">
    <div class="divWrapper"> 
        <div class="link">
            <asp:LinkButton ID="lbPublic" runat="server" Text="Public" ClientIDMode="Static" />
                &nbsp;
            <asp:LinkButton ID="lbPrivate" runat="server" Text="Private" ClientIDMode="Static" />
        </div>
        <asp:ListView ID="ImageListView" runat="server">
            <LayoutTemplate>
                <ul class="ImageListView">
                    <asp:PlaceHolder runat="server" ID="itemPlaceholder" />
                </ul>
            </LayoutTemplate>
            <ItemTemplate>
                <li style="margin-left: 0px;">
                    <a class="photo" href="#">
                        <asp:Image ID="thumbnail" runat="server" Height="128" Width="128" ImageUrl='<%# Container.DataItem  %>' />
                    </a>
                </li>
            </ItemTemplate>
            <EmptyItemTemplate>
                <div>
                    Sorry! No image found.
                </div>
            </EmptyItemTemplate>
        </asp:ListView>   
        <div style="width:300px; margin:0 auto;">
            <table class="tblMain" style="width: 100%; border-collapse: collapse;">
                <tr>
                    <td>
                        <div class="datapager">   
                            <asp:DataPager ID="DataPager1" PageSize="45" PagedControlID="ImageListView" runat="server" ClientIDMode="Static">
                                <Fields>
                                    <asp:NumericPagerField />
                                </Fields>
                            </asp:DataPager>     
                        </div>
                    </td>
                </tr>                       
            </table>
        </div>      
    </div>
</form>
</body>
</html>

StyleSheet1.css

 .divWrapper{
margin:80px 50px 0px 50px;
position: relative;
 }
 .divWrapper a img{
border:none;
 }
 .divWrapper ul{
list-style:none;
padding-bottom:20px;
float:left;
 }
 .divWrapper ul li{
position:relative;
text-align:center;
float:left;
margin:3px;
background-color:#121212;
width:180px;
height:140px;
border:1px solid #292929;
-moz-border-radius:5px;
-webkit-border-radius:5px;
border-radius:5px;
}
.divWrapper ul li a{
display:table-cell;
text-align:center;
vertical-align:middle;
width:180px;
height:140px;
outline:none;
-moz-border-radius:5px;
-webkit-border-radius:5px;
border-radius:5px;
-moz-box-shadow:0px 0px 3px #000 inset;
-webkit-box-shadow:0px 0px 3px #000 inset;
box-shadow:0px 0px 3px #000 inset;
}
.divWrapper ul li a:hover{
background-image:none;  
 }
.divWrapper ul li a img{
vertical-align:middle;
border:1px solid #222;
opacity:0.6;
filter:progid:DXImageTransform.Microsoft.Alpha(opacity=60);
-moz-box-shadow:1px 1px 3px #000;
-webkit-box-shadow:1px 1px 3px #000;
box-shadow:1px 1px 3px #000;
}
 .divWrapper ul li a.photo{
background:transparent url(../images/photo.png) no-repeat top right;
}

1 个答案:

答案 0 :(得分:0)

结果显示datapager startRowIndex保留旧的数据源值。如果列表视图的项目少于前一个,则必须更新此值。您可以创建一个会话变量来跟踪不同的数据源datapager startRowIndex,或者只是将其重置为第一页,如下所示:

Protected Sub lbPublic_Click(sender As Object, e As EventArgs) Handles lbPublic.Click
    Session("Mode") = "Public"
    Me.DataPager1.SetPageProperties(0, me.DataPager1.maximumRows, False)
End Sub

Protected Sub lbPrivate_Click(sender As Object, e As EventArgs) Handles lbPrivate.Click
    Session("Mode") = "Private"
    Me.DataPager1.SetPageProperties(0, me.DataPager1.maximumRows, False)
End Sub