Reputation: 79
I'm trying to bind a List to a Repeater. Here is my code:
protected void Page_Load(object sender, EventArgs args)
{
CaseEnt MyCase = MyLib.IAdapters.CaseAdapter.GetCaseRecordForCaseNumber("14CF000810");
List<Charge> charges = MyLib.IAdapters.ChargeAdapter.GetByCaseNumber("14CF000810");
if (charges != null && charges.Count > 0)
{
foreach (Charge chg in charges)
{
List <Sentence> sentences = MyLib.IAdapters.SentenceAdapter.GetByChargeObjectID(chg.ChargeObjectID);
rpt1.DataSource = sentences;
rpt1.DataBind();
}
}
}
I've put stops in the code to check what values are flowing into the Lists. When the sentences List is being populated, it shows values, but when I check it when it's being assigned as the Datasource, it's blank.
What am I doing wrong?
I've been working at this for a while, and I've scoured the internet for answers, and no one seems to be able to help me. Any assistance would be appreciated, even if it's explaining a different way of doing this.
Upvotes: 0
Views: 1248
Reputation: 417
Rational behind assigning a datasource to repeater in the foreach
loop is hard to understand.
Every time the repeater is assigned the new set of sentences as a datasource, the previously bound data (i.e., sentences) will be removed, new sentences won't be appended.
In your case, the last instance of charge - chg
, doesn't have any sentences and hence the value of List <Sentence> sentences
inside foreach
loop becomes null
. That's the only case I can think of why it's behaving like this. You may want to put a breakpoint on this line to see if that's the case -
rpt1.DataSource = sentences;
If you want all the sentences from all the charges to be bound to Repeater control, you may want to collect all of them by your CaseNumber first, and then bind it once without foreach
loop.
UPDATE:
You have written 2 methods - one to get list of charges for a given case number, and second, get a list of sentences for a given charge.
Instead, you can do either of these two-
Write only one method that will get all the sentences for all the charges for a given case number. Get the result of this method in List <Sentence>
object and bind it with Repeater control directly.
Don't write any new method, but instead of binding a repeater in foreach
loop, you can keep adding sentences for each charge in List <Sentence>
object.
Like this:
List <Sentence> sentences;
foreach (Charge chg in charges)
{
List <Sentence> sentencesForThisCharge = MyLib.IAdapters.SentenceAdapter.GetByChargeObjectID(chg.ChargeObjectID);
if (null != sentencesForThisCharge)
{
sentences.Add(sentencesForThisCharge);
}
}
rpt1.DataSource = sentences;
rpt1.DataBind();
I hope this helps.
Upvotes: 1
Reputation: 2494
You are resetting the bind to the repeater, effectively overwriting what is already bound to it.
If you remove your sentences list and databind command outside of the loop it will work.
if (charges != null && charges.Count > 0)
{
List<Sentence> sentences = new List<Sentence>();
foreach (Charge chg in charges)
{
sentences.AddRange(MyLib.IAdapters.SentenceAdapter.GetByChargeObjectID(chg.ChargeObjectID))
}
rpt1.DataSource = sentences;
rpt1.DataBind();
}
Upvotes: 0