1
votes

It looks like several questions on this have been posted, but none seem to quite fix my problem. I have a user control inside a repeater. The user control has one public property with just a getter/setter. The databinding happens fine on the initial page load, but when selecting a filter from a dropdown menu, the first repeater binds okay, but the second repeater containing the user control shows null for the property. So, on to the actual code:

My two repeaters look like this:

<asp:Repeater ID="rptTransactionVisual" runat="server" OnItemDataBound="rptTransactionVisual_ItemDataBound">
    <ItemTemplate>
        <%#Eval("TransactionAmount")%> 

^--I had this in for a sanity check - it always gets a value, even when transaction below receives a null value, so I know it's getting a datasource and being databound

        <st:visual transaction="<%#(Transaction)(Container.DataItem)%>" runat="server" />
    </ItemTemplate>
</asp:Repeater>

<asp:Repeater ID="rptTransactions" runat="server" OnItemDataBound="rptTransactions_ItemDataBound" OnDataBinding="rptTransactions_DataBinding">
    <ItemTemplate>
        <tr>
            <td><%#DataBinder.Eval(Container.DataItem, "TransactionDate", "{0:MM/dd/yyyy}")%></td>
            <td><asp:Literal ID="ltlTransactionAmount" runat="server" /></td>
            <td><asp:Literal ID="ltlClient" runat="server" /></td>
            <td><asp:Literal ID="ltlTransactionType" runat="server" /></td>
            <td><asp:Literal ID="ltlSector" runat="server" /></td>
        </tr>
    </ItemTemplate>
</asp:Repeater>

I am binding the value of the top repeater to the datasource of the second repeater since they are the same:

protected void rptTransactions_DataBinding(object sender, EventArgs e)
    {
        rptTransactionVisual.DataSource = rptTransactions.DataSource;
        rptTransactionVisual.DataBind();
    }

Here is the relevant code from the user control, which works fine initially, but after filtering, transaction is always null;

public Transaction transaction { get; set; }

    protected override void OnLoad(EventArgs e)
     {
        base.OnLoad(e);

        if (transaction == null) return;
....

Here is the code for the filtering:

protected void ddTransactionType_SelectedIndexChanged(object sender, EventArgs e)
    {
        var transactions = getChildPages().Where(p => p.TransactionType == ddTransactionType.SelectedValue).ToList<TransactionPage>();
        }

        rptTransactions.DataSource = transactions;
        rptTransactions.DataBind();
       //databinding happens always for the list repeater, and I do see the visual repeater being databound with the correct datasource
    }

I've beaten my head against the wall on this.

1

1 Answers

1
votes

I still don't know why the original one was null after postback, but I did solve it by a little rework of the Visual repeater ItemDataBound event.

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            TransactionPage transaction = (TransactionPage)e.Item.DataItem;
            TransactionPagePartial visual = (TransactionPagePartial)Page.LoadControl("~/Views/Pages/Partials/TransactionPagePartial.ascx");
            visual.transaction = transaction;
            rptTransactionVisual.Controls.Add(tombstone);
        }