1

I'm trying to create a simple pager user control which will be used in a footer of an ASP.NET repeater.

Everytime the user clicks Next I would like to increment the PageCounter by 10 and save the value in ViewState. It works the first time I click on lbNext but afterwards it keeps getting reset to 10 and ViewState["PageCounter"] is null

I've tried decorating my property with [PersistenceMode(PersistenceMode.Attribute)] but still no luck. Why is the value not being persisted to ViewState?

ASCX:

<%@ Control Language="C#" EnableViewState="true" AutoEventWireup="true" CodeBehind="Pager.ascx.cs" Inherits="WebApplication6.Pager" %>
<asp:LinkButton ID="lbPrevious" runat="server" OnCommand="SetPage" CommandArgument="Previous">Previous</asp:LinkButton>
<asp:TextBox ID="txtPage" runat="server" EnableViewState="true" Enabled="false"></asp:TextBox>
<asp:LinkButton ID="lbNext" runat="server" OnCommand="SetPage" CommandArgument="Next">Next</asp:LinkButton>

Code behind:

public partial class Pager : System.Web.UI.UserControl
{
    [PersistenceMode(PersistenceMode.Attribute)]
    public int PageCounter
    {
        get
        {
            var counter = ViewState["PageCounter"];
            return ((counter == null) ? 0 : (int)counter);
        }
        set
        {
            ViewState["PageCounter"] = value;
            txtPage.Text = value.ToString();
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            PageCounter = 0;
    }

    protected void SetPage(object sender, CommandEventArgs e)
    {
        switch (e.CommandArgument.ToString())
        {
            case "Previous":
                PageCounter -= 10;
                break;
            case "Next":
                PageCounter = PageCounter + 10;
                break;
        }
    }
}

EDIT:

Here's the code where I add the Pager user control to the repeater:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Repeater.aspx.cs" Inherits="WebApplication6.Repeater" %>
<%@ Register Src="Pager.ascx" TagName="Pager" TagPrefix="asp" %>
<!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>Repeater control</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:Repeater ID="booksRepeater" runat="server">
        <ItemTemplate>
            <table>
                <tr>
                    <td>
                        Name:&nbsp;<asp:Label ID="lblName" runat="server" Text='<%# Bind("FirstName") %>' />
                    </td>
                </tr>
                <tr>
                    <td>
                        Surname:&nbsp;<asp:Label ID="lblSurname" runat="server" Text='<%# Bind("LastName") %>' />
                    </td>
                </tr>
            </table>
            </div>
        </ItemTemplate>
        <FooterTemplate>
            <asp:Pager ID="pager" runat="server" />
        </FooterTemplate>
    </asp:Repeater>
    </form>
</body>
</html>
xpda
  • 15,585
  • 8
  • 51
  • 82
Denys Wessels
  • 16,829
  • 14
  • 80
  • 120
  • Could you post the code of where/how you add the `Pager` to the repeater/page? – Chris Sinclair Jan 05 '13 at 20:13
  • When I used to develop in ASP.NET, I don't recall using the `PersistenceMode`. From a bit of googling, it seems like it's used to have the control persist its data automatically without view state (or leverages viewstate/session state behind the scenes). Possibly the old value is being automatically persisted and overwriting the Viewstate's value with `0` when ASP.NET rebuilds the controls on post back. What happens if you ditch the `PersistenceMode` attribute? Also, try running the debugger and see if the `setter` is firing with old data than what you expect, or out of order. – Chris Sinclair Jan 05 '13 at 20:30
  • I've tried it initially without PersistenceMode, it doesn't work.The setter is is fine, the problem is ViewState is being reset in the getter. If you recreate the control you'll see what I'm talking about – Denys Wessels Jan 06 '13 at 07:42

1 Answers1

1

Your code does not show when your data binds to your Repeater ("booksRepeater"). Make sure you bind your data in time. It sounds like this problem: Maintaining viewstate of a repeater

Community
  • 1
  • 1
Jacco
  • 3,251
  • 1
  • 19
  • 29