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

  2. "sgh" <> wrote in message
    news:...
    ....
    > 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?


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

  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.
    >-----Original Message-----
    >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?
    >.
    >
     
    Weston Weems, Dec 1, 2004
    #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. Kel Good
    Replies:
    2
    Views:
    13,276
    Kel Good
    Apr 25, 2004
  2. Peter Rilling
    Replies:
    1
    Views:
    1,065
    =?Utf-8?B?RWx0b24gVw==?=
    May 19, 2005
  3. =?Utf-8?B?RXJpYw==?=

    Repeater control -- Using e.Item.DataItem

    =?Utf-8?B?RXJpYw==?=, May 10, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    17,582
    ichi27
    Jun 2, 2011
  4. Jeff

    newbie: e.Item.DataItem = null

    Jeff, Jul 10, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    4,808
    Karl Seguin [MVP]
    Jul 11, 2006
  5. Dave Adams

    Repeater e.item.dataitem null on postback

    Dave Adams, Oct 13, 2003, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    504
    Dave Adams
    Oct 13, 2003
Loading...

Share This Page