0

This is my C# Backend:

protected void Page_Load(object sender, EventArgs args)
        {

            CaseEnt MyCase = McHenryLib.ICISAdapters.CaseAdapter.GetCaseRecordForCaseNumber(Globals.CaseNum);
            List<Charge> charges = McHenryLib.ICISAdapters.ChargeAdapter.GetByCaseNumber(Globals.CaseNum);

            rptChg.DataSource = charges;
            rptChg.DataBind();

        }

       protected void rptChg_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            CaseEnt MyCase = McHenryLib.ICISAdapters.CaseAdapter.GetCaseRecordForCaseNumber(Globals.CaseNum);
            List<Charge> charges = McHenryLib.ICISAdapters.ChargeAdapter.GetByCaseNumber(Globals.CaseNum);

            if (charges != null && charges.Count > 0)
            {
                List<Sentence> sentences = new List<Sentence>();

                foreach (Charge chg in charges)
                {
                    Repeater rptSen = (Repeater)(e.Item.FindControl("rptSen"));

                    sentences.AddRange(McHenryLib.ICISAdapters.SentenceAdapter.GetByChargeObjectID(chg.ChargeObjectID));

                    if (sentences != null && sentences.Count > 0)
                    {
                        List<SentenceDisposition> disps = new List<SentenceDisposition>();

                        foreach (Sentence sen in sentences)
                        {

                            Repeater rptDisp = (Repeater)(e.Item.FindControl("rptDisp"));
                            disps.AddRange(McHenryLib.ICISAdapters.SentenceDispositionAdapter.GetDispositionsBySentenceOID(sen.SentenceID));

                            rptDisp.DataSource = disps;
                            rptDisp.DataBind();
                        }
                    }

                    rptSen.DataSource = sentences;
                    rptSen.DataBind();
                }
            }
         } 

This is my ASP.NET frontend:

<asp:Repeater ID="rptChg" runat="server" OnItemDataBound="rptChg_ItemDataBound">
<ItemTemplate>
<br />

<table class="chg">
    <tr>
        <th>Count <%#Eval("Counts") %></th>
    </tr>
</table>

<!--Builds the Charges Table-->
<table class="chg">
    <tr>
        <th>Charge Date</th>
        <th>Statute</th>
        <th>Description</th>
        <th>Disposition/Manner</th>

    </tr>
    <tr>
        <td><%#Eval("ChargeDate")%></td>
        <td><%#Eval("Statute")%></td>
        <td><%#Eval("StatuteDesc")%></td>
        <td><%#ChgCode(Eval("Disposition"))%></td>
    </tr>
</table>

<!--Builds the Sentences Table-->
<table class="sen">
    <asp:Repeater ID="rptSen" runat="server">
        <HeaderTemplate>
            <th colspan="3">Sentences</th>
        </HeaderTemplate>
        <ItemTemplate>  
            <tr>
                <td><%#Eval("SentenceDate")%></td>
                <td><%#Eval("TypeDesc")%></td>
                <td><%#ZeroToNull(Eval("UnitTerm"))%> <%#ZeroToNullMoney(Eval("Amount"))%> <%#Eval("UnitTypeDesc")%></td>
            </tr>
        </ItemTemplate>
    </asp:Repeater>
</table> 

I'm trying to get the Sentences table to repopulate for every new instance of the Charge table, with nested repeaters. However, I've tried a couple different solutions but every time the Sentences table just spits out the same data from the first charge onto all of them.

I've stepped through the code a few times, and I noticed that when it goes to DataBind the second Sentences table, the repeater's ClientID doesn't change, which makes me think that it's not rolling over a new instance of the repeater every time, and is instead just repeating the same data every time.

My question is: is there a way to have it manually roll a new sequential ClientID, so that it binds to the correct repeater each time? Or am I going about this completely the wrong way entirely?

1
  • This is killing the database server. MyCase and charges could be retrieved once if you make them members of the page class. Populate them in the page load event and then use them in the databound events. MyCase doesn't seem to be used by the databound mehtod though. Aug 30, 2017 at 15:18

1 Answer 1

0

First remove the unnecessary calls fro MyCase and charges in the rptChg_ItemDataBound method. You don't need to load the same over and over again for every bound Charge item.

You can access the Charge item via the RepeaterItemEventArgs object passed to you in the handler method.

protected void rptChg_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
       //check if it is a ItemTemplate. Sometimes could be a header template?
       if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
       {
         var charge = (Charge)e.Item.DataItem;
         Repeater rptSen = (Repeater)(e.Item.FindControl("rptSen"));

        var sentences = SentenceAdapter.GetByChargeObjectID(chg.ChargeObjectID); // notice I removed the namespace for clarity, use the using statements instead

        rptSen.DataSource = sentences;
        rptSen.DataBind();
      }
    }

What is missing from you front, but you want to achieve from the code behind is the second repeater of the SentenceDispositions. This would probably go in the ItemTemplate of the Sentences repeater. Following the same pattern you can handle the ItemDataBound event of the rptSen repeater.

protected void rptSen_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        var sentence = (SentenceDisposition)e.Item.DataItem;
        Repeater rptDisp = (Repeater)(e.Item.FindControl("rptDisp"));

        var dispositions = SentenceDispositionAdapter.GetDispositionsBySentenceOID(sentence.SentenceID));

        rptDisp.DataSource = dispositions ;
        rptDisp.DataBind();
    }
}

https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemdatabound(v=vs.110).aspx

5
  • Alright, thank you for the answer. I do have a lingering question though, where do I get chg from? In the old code it was in the Foreach block, where do I assign it here? As is, it says chg does not exist in this context.
    – Taborator
    Aug 30, 2017 at 15:52
  • Welcome. rptChg remains as it is. It is populated with data in the Page_Load. Aug 30, 2017 at 15:54
  • I'm sorry, I wasn't clear enough. I meant in SentenceAdapter.GetByChargeObjectID(chg.ChargeObjectID); It is telling me chg does not exist in this context.
    – Taborator
    Aug 30, 2017 at 15:55
  • I see what you mean. I renamed chg to charge. See how charge looks now. You don't need a foreach. That's the whole point. Aug 30, 2017 at 15:56
  • You used the foreach to obtain a chg, but is not ok. chg was alread there, hidden in the e.Item.DataItem Aug 30, 2017 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.