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

Discussion in 'ASP .Net Web Controls' started by Thomas Nielsen [AM Production A/S], Dec 1, 2003.

  1. 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
    Thomas Nielsen [AM Production A/S], Dec 1, 2003
    #1
    1. Advertising

  2. Thomas Nielsen [AM Production A/S]

    MSFT Guest

    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.)
    MSFT, Dec 2, 2003
    #2
    1. Advertising

  3. 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

    "MSFT" <> wrote in message
    news:...
    > 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.)
    >
    Thomas Nielsen [AM Production A/S], Dec 2, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?S2V2aW4gSGVjdG9y?=

    Repeater ItemCreated vs ItemDataBound

    =?Utf-8?B?S2V2aW4gSGVjdG9y?=, Apr 11, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    9,892
    Teemu Keiski
    Apr 11, 2004
  2. Dennis E. Jones, Jr.

    Repeater - ItemDataBound and ItemCreated

    Dennis E. Jones, Jr., Nov 18, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    15,884
    Dennis E. Jones, Jr.
    Nov 18, 2005
  3. Replies:
    3
    Views:
    1,901
  4. Philippe Camus
    Replies:
    2
    Views:
    2,495
    Philippe Camus
    Mar 30, 2006
  5. Grzegorz Chrupala
    Replies:
    2
    Views:
    194
    Grzegorz Chrupala
    Jun 30, 2003
Loading...

Share This Page