我正在尝试通过将 aNested Repeater
数据绑定到 a 来填充 a Generic List
。
用户将能够随意添加/删除值Generic List
。
这一切都被整齐地包裹在一个Ajax Update Panel
.
我现在遇到一个问题,当用户尝试向 中添加值时Generic List
,它只添加 ` 初始值,然后在他们单击以添加更多值时不断更新该值。
我怀疑这是因为Generic List
它没有保留其初始数据或其他东西;但我真的不太确定。
请有人帮忙。
挑战.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;
}
}
}
页面.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();
}
更新:中继器的绑定(同一页面)
/// <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();
}