I am trying to populate a Nested Repeater
by databinding it to a Generic List
.
The user will be able to add / remove values from this Generic List
at will.
All this is neatly wrapped in an Ajax Update Panel
.
I am now having a problem that when the user tries to add values to the Generic List
, it only adds ` initial value, then keeps updating that value when they click to add more.
I have a suspicion that its because the Generic List
doesn't keel its initial data or something; but I'm really not too sure.
Please can someone help.
Chalenge.cs
public class challenge
{
public class Team
{
public string TeamName { get; set; }
}
public class Member:Team
{
public string Name { get; set; }
}
//This is just a test, but I cant get a list in a class file to work properly.
public class ChallengeList
{
public List<Member> Member()
{
return null;
}
}
}
Page.cs
private List<challenge.Member> Members = new List<challenge.Member>();
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack) return;
lblCurrent.Text = SiteSession.Current.Nickname;
lblChallenge.Text = Request.QueryString["Challenge"];
//Add the initial teams
PopulateTeamRepeater();
AddData();
}
private List<challenge.Member> AddData()
{
Members.Add(
new challenge.Member
{
Name = SiteSession.Current.Nickname,
TeamName = "Team One"
});
Members.Add(
new challenge.Member
{
Name = Request.QueryString["Challenge"],
TeamName = "Team Two"
});
return Members;
}
/// <summary>
/// Add the selected member to the team
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void drpTeams_SelectedIndexChanged(object sender, EventArgs e)
{
Members.Add(
new challenge.Member
{
Name = tbMemberAuto.Text,
TeamName = drpTeams.SelectedItem.Value
});
PopulateTeamRepeater();
}
UPDATE: Binding of the repeaters (Same page)
/// <summary>
/// Populates the Team repeater control with the amount of teams the user has selected
/// </summary>
private void PopulateTeamRepeater()
{
ArrayList cArrayList = new ArrayList(); //Main holder
StringBuilder sb = new StringBuilder(); //Temp holer
try
{
//Get all team numbers
foreach (challenge.Member Team in AddData())
{
//Add it to the string builder
sb.Append(Team.TeamName + ";");
}
//Split the string to get the strings (As a temporary holder)
string[] cs = sb.ToString().Split(';');
//Add the groups (unique) to the next holder
foreach (string s in cs.Where(s => !cArrayList.Contains(s)))
{
cArrayList.Add(s);
}
rptTeam.DataSource = cArrayList;
rptTeam.DataBind();
}
catch (Exception es)
{
misc.ChangeInfo(Master.Page, "Error filling teams: " + es.Message, "ui-state-error");
}
}
/// <summary>
/// Populate the Member repeater based on the Team
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void rptTeam_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
Repeater rptMembers = (Repeater)e.Item.FindControl("rptMembers");
Label lblTeam = (Label)e.Item.FindControl("lblTeam");
rptMembers.DataSource = AddData().Where(m => m.TeamName == lblTeam.Text);
rptMembers.DataBind();
}