我尝试通过将Nested Repeater
数据绑定到Generic List
来填充Generic List
。
用户可以随意添加/删除此Ajax Update Panel
中的值
所有这些都整齐地包裹在Generic List
。
我现在遇到的问题是,当用户尝试向Generic List
添加值时,它只会添加`初始值,然后在点击添加更多值时不断更新该值。
我怀疑它是因为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;
}
}
}
没有记录其初始数据或其他内容;但我真的不太确定。
请有人帮忙。
Chalenge.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();
}
Page.cs
/// <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();
}
更新:转发器的绑定(同一页面)
{{1}}
答案 0 :(得分:2)
您可以将它们保持在视图状态:
const string cChalMemNameConst = "ChalMem_cnst";
public List<challenge.Member> Members
{
get
{
if (!(ViewState[cChalMemNameConst] is List<challenge.Member>))
{
// need to fix the memory and added to viewstate
ViewState[cChalMemNameConst] = new List<challenge.Member>();
}
return (List<challenge.Member>)ViewState[cChalMemNameConst];
}
}
同时在您[Serializable]
上添加List<>
类的[Serializable]
public class challenge
{
[Serializable]
public class Team
{
public string TeamName { get; set; }
}
[Serializable]
public class Member:Team
{
public string Name { get; set; }
}
[Serializable]
public class ChallengeList
{
public List<Member> Member()
{
return null;
}
}
}
属性:
private List<challenge.Member> Members = new List<challenge.Member>();
注意:必须通过以上语句将行{{1}}更改为将其保存到viewstate。