Reputation: 6597
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
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:
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
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
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