On post back repeater's Item.DataItem are all null (?)

Discussion in 'ASP .Net Web Controls' started by sgh, Dec 1, 2004.

  1. sgh

    sgh Guest

    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;

    ...

    "row" (or item.DataItem) is null at this point...why?
     
    sgh, Dec 1, 2004
    #1
    1. Advertisements

  2. ....
    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
     
    John Saunders, Dec 1, 2004
    #2
    1. Advertisements

  3. sgh

    Weston Weems Guest

    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.
     
    Weston Weems, Dec 1, 2004
    #3
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.