C# 如何使用嵌套中继器动态添加/删除数据
我正在开发ASP.NET Web表单应用程序。业务逻辑是-我有很多用户,每个用户都有几个帐户。我有一个视图,系统用户可以在其中添加人员,当添加人员时,我想显示我为此选择的表,该人员的姓名以及与他相关的所有帐户。因此,最终输出应该类似于:C# 如何使用嵌套中继器动态添加/删除数据,c#,asp.net,C#,Asp.net,我正在开发ASP.NET Web表单应用程序。业务逻辑是-我有很多用户,每个用户都有几个帐户。我有一个视图,系统用户可以在其中添加人员,当添加人员时,我想显示我为此选择的表,该人员的姓名以及与他相关的所有帐户。因此,最终输出应该类似于: <span>Person name</span> <table> <tr>Account 1 info..</tr> <tr>Account 2 info..</tr> .. &
<span>Person name</span>
<table>
<tr>Account 1 info..</tr>
<tr>Account 2 info..</tr>
..
</table>
<span>Another Person name</span>
<table>
<tr>Account 1 info..</tr>
<tr>Account 2 info..</tr>
..
</table>
及
最后,变量feedAccountRptr是类字典feedAccountRptr;中的一个类成员 GetAccountsForClient看起来像什么?我认为在该方法中,您可能希望传递与父repeater项相关的某种参数。@j.f.是的,它使用新添加的人的id在数据库上执行简单选择。我使用这种方法来获取每个新人的数据。旧数据存储在ViewState变量中。该ViewState键是否在GetAccountsForClient中的任何位置使用?我们能看到那个密码吗?我仍然不能完全确定ViewState键用于什么,或者为什么需要它;和ViewState[theAccounts]=feedAccountRptr;-ViewState的使用现在有意义吗?我知道你是如何使用它的。我不完全明白你为什么要用它。我猜GetAccountsForClient中有一些可疑的东西,因为从那里返回的是绑定到内部中继器的东西。
<asp:Repeater id="RpAccountsToDistraint"
OnItemDataBound="RpAccountsToDistraint_ItemDataBound"
runat="server">
<ItemTemplate>
<fieldset><legend>Person name</legend>
<asp:Repeater ID="RpInnerAccountsToDistraint"
OnItemDataBound="RpInnerAccountsToDistraint_ItemDataBound"
runat="server">
<HeaderTemplate>
<table>
<thead>
<tr>
<th>Account</th>
</tr>
</thead>
</HeaderTemplate>
<ItemTemplate>
<tbody>
<tr>
<td>Account Amount..</td>
..
</tbody>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
protected void ParentRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem
|| e.Item.ItemType == ListItemType.Item)
{
Repeater innerRepeater =
(Repeater)e.Item.FindControl("RpInnerAccountsToDistraint");
if (null == feedAccountRptr)
{
feedAccountRptr = new Dictionary<int, List<ClientAccount>>();
}
if (!(((Dictionary<int, List<ClientAccount>>)ViewState["theAccounts"]) == null))
{
feedAccountRptr = ((Dictionary<int, List<ClientAccount>>)ViewState["theAccounts"]);
}
int count = feedAccountRptr.Count;
feedAccountRptr.Add(++count, GetAccountsForClient());
ViewState["theAccounts"] = feedAccountRptr;
innerRepeater.DataSource = GetAccountsForClient();// feedAccountRptr;
innerRepeater.DataBind();
}
protected void InnerRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem
|| e.Item.ItemType == ListItemType.Item)
{
//Just take the values from my controls but actually not doing anything else