asp listview datasource not updating

169 views Asked by At

If I run this project, click the 3rd page of the datapager, then click my "private" link, which binds a new list to the listview, it displays nothing. The debugger shows that the datasource count = 9 is correct. The Datasource items count should also be 9, but its 0. This is the problem, but I cant really figure out why this is happening. Any ideas?

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

There are 1 answers

0
user3585893 On

Turns out the datapager startRowIndex keeps the old datasources value. You have to update this value if your listview has less items than the previous one. You can create a session variable to track your different datasources datapager startRowIndex or just reset it to the first page like this:

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