Groups | Blog | Home
all groups > asp.net webcontrols > november 2004 >

asp.net webcontrols : On post back repeater's Item.DataItem are all null (?)


shodson NO[at]SPAM gmail.com
11/30/2004 5:31:02 PM
I have a repeater like this

<asp:Repeater id="rptPremiums" EnableViewState="True"
OnItemDataBound="rptPremiums_ItemDataBound" runat="Server" >

For brevity's sake, suffice it to say there are header, item and
footer templates defined here as well.

And I bind to it in my code-behind like this

========================================

protected void rptPremiums_ItemDataBound(object sender,
RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
DataRowView row = (DataRowView) e.Item.DataItem;

....

yada yada, FindControl() for each control in my item template, set the
values from the row (from a DataTable), etc.

So, when I press a save button (btnSave) I want to iterate through the
repeater's items directly, but when I do the DataItem for each Item is
null. Why is my data not being preserved on post back? I have
EnableViewState="True" on the repeater control.

protected void btnSave_Click(object sender, EventArgs e)
{
foreach (RepeaterItem item in rptPremiums.Items)
{
if (item.ItemType == ListItemType.Item || item.ItemType ==
ListItemType.AlternatingItem)
{
DataRowView row = (DataRowView) item.DataItem;

...

John Saunders
11/30/2004 9:58:42 PM
[quoted text, click to view]

Your data isn't supposed to be preserved on PostBack. Properties of controls
are supposed to be preserved.

For instance, if you had a Label within the repeater, and it was databound
to some value, then you'd find the same value on PostBack, but DataItem
would be null. It only has a reliable value during DataBinding.


John Saunders

Weston Weems
12/1/2004 12:59:09 PM
Couple Bits of Advice...

a) What John said, the data source items are only
avaliable when databound. They are no longer accessable
after initial loading.

You might consider having an object or object collection
representing onscreen data that you update with the grid,
then persist changes from that to databases.

b) I've been made aware that in datatablse where large
number of records occur, that method of databinding isnt
the best way to go.. instead use a method to fetch values
out of the databinder...

protected string ShowData(object data, string col)
{
DocumentPart dp = (DocumentPart) data;
switch(col)
{
case "Effective":
return dp.Effective.ToString("MM/dd/yy");
break;
}

then you can call with...
<%#ShowData(Container.DataItem, "Effective")%>

Requires a bit of upkeep, but gives you full control of
formatting and as far as my experience goes, performs much
better.

Also, I can bind all sortsa custom datatypes to my
datagrids and still pull values out from the codebehinds.
[quoted text, click to view]
AddThis Social Bookmark Button