debracey
debracey

Reputation: 6597

ASP.NET Repeater with UserControl & Postback

I have an ASP.NET Repeater with a custom user control defined as follows:

<asp:Repeater ID="feedCards" runat="server">
  <ItemTemplate>
    <vsan:card ID="card" VFeed='<%# Container.DataItem %>' runat="server" />
  </ItemTemplate>
</asp:Repeater>

The repeater is databound in the code behind OnInit with the standard method:

List<someObject> data = ...;
feedCards.DataSource = data;
feedCards.DataBind();

This works fine until I introduce the need for periodic updating of the data on the page. There's an asp:Timer on the page that fires every 30 seconds. When the timer fires it checks some server side data an updates the page if necessary.

The problem is, that when the timer fires, I lose all of the data in each of the repeater's cards. The correct # of cards are displayed, but they have no data in them.

Is there a way to have the user control maintain its data through asp.net's postback?

Upvotes: 0

Views: 1687

Answers (3)

debracey
debracey

Reputation: 6597

There were several problems that I had to overcome, here's what I ended up doing:

ASP.NET was not storing my object in the session information, even though I had EnableViewState and ViewStateMode at either Enabled or True. To get around this, I changed my property slightly

object realObj = null;
public object VFeed
{
   get { return realObj; }
   set
   {
      realObj = value;
      ViewState["objectID"] = realObj.ID;
   }
}

Then I could pull back the real object in Page_Load of the user control by pulling the object back out of the database (using the saved ViewState ID).

Then there was a problem with the way my timer was implemented. I tried several things:

  1. Just putting the timer on the page, not contained in any kind of update panel, etc.
    • This didn't work as the entire page refreshed, and so did all of my user controls
  2. Setting the timer as a trigger for the update panel that contains my list of items
    • That worked better - as only that section of the page refreshed, but in this case that wasn't sufficient
    • I don't want all my fancy user controls blinking every 30 seconds.

What ended up working was to put the timer in its own update panel, with the timer set as the trigger to the update panel. I found the solution here, here's the code

<asp:UpdatePanel runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false"
    ID="upTimer">
    <ContentTemplate>
        <asp:Timer ID="GameClock" runat="server" Interval="5000" Enabled="true" OnTick="GameClock_Tick">
        </asp:Timer>
    </ContentTemplate>
</asp:UpdatePanel>

Upvotes: 0

Garrison Neely
Garrison Neely

Reputation: 3289

In your OnInit method, what happens if you save the List<someObject> you retrieve to ViewState as well as binding it? That way, when your Timer Ticks, if you don't have new data, you just rebind to the stored data in ViewState.

Upvotes: 1

coutol
coutol

Reputation: 338

If I understood correctly your question the data is bounded but does not appear in your repeater right? So this code may help you to solve it:

protected void yourRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    MyDataBoundedObject bounded = (MyDataBoundedObject)e.Item.DataItem;
    Label lbText = (Label)e.Item.FindControl("myText");
    lbText.Text = bounded.myText;
}

Upvotes: 1

Related Questions