Repeater/ViewState: how to preserve properties on controls set on ItemCreated?

  • Thread starter Thomas Nielsen [AM Production A/S]
  • Start date
T

Thomas Nielsen [AM Production A/S]

Hi,

Whenever i bind data to a repeater, i use the repeater ItemCreated event to
be able to properly format the data from the datasource into the repeater.

Example, codefront:
<asp:Repeater id="myRepeater" Runat="server" EnableViewState=True>
<ItemTemplate>
<tr>
<td class="listCell"><asp:Literal Runat=server
ID="ReferenceData"></asp:Literal></td>
</tr>
</ItemTemplate>
</asp:Repeater>

Example, codebehind:
public void RecordList_ItemCreated(object myObject, RepeaterItemEventArgs
args)
{
myString =
((string)((DataRowView)args.Item.DataItem)["ReferenceData"]).ToString();
((Literal)args.Item.FindControl("ReferenceData")).Text = myString;
}

This allows me to format the data from the datasource in any way i prefer,
which i like.

However, it looks like the properties i set in the codebehind are not stored
in the ViewState, meaning that these are not preserved at postback. If i
enable ViewState on the repeater, structure and objects of the repeater are
preserved fine on postback, but in this example, Reference does not contain
the .text i originally set in the RecordList_ItemCreated event.

Is there a way to preserve these values in the ViewState?. I would also
appreciate any advice on best pactices in this scenario; I've seen examples
where the data is re-bound on postback, which i really cannot accept due to
performance issues.

Cheers,

/Thomas
 
M

MSFT

Hi Thomas,

If you move the code into the repeater's ItemDataBound method, will the
value be kept?

Luke
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
T

Thomas Nielsen [AM Production A/S]

To which address should i send your christmas card? ;)

Thanks Luke. Looks like it solved the problem. You've made a handfull of
frustrated programmers .. well.. less frustrated :).

/Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top