[英]ASP.NET populate lists referencing each other on selected index change
I have an ASP.NET control with three listboxes: LBParent
, LBChild1
and LBChild2
. 我有一个带有三个列表框的ASP.NET控件:
LBParent
, LBChild1
和LBChild2
。
LBParent
has to be filled programmatically. LBParent
必须以编程方式填充。 I do this in Page_Load
. 我在
Page_Load
执行此操作。
When the user selects a value in LBParent
, I want to fill both LBChild1
and LBChild2
with some data, programmatically. 当用户在
LBParent
选择一个值时,我想以编程方式用一些数据填充LBChild1
和LBChild2
。 The children lists must also keep track of their selected value. 子级列表还必须跟踪其选定的值。
Basically, the parent list is a list of users, the first list is a list of permissions the user has, the second list is a list of permissions the user doesn't have. 基本上,父列表是用户列表,第一个列表是用户拥有的权限列表,第二个列表是用户没有的权限列表。
My plan is to add two buttons to move permissions between the two lists. 我的计划是添加两个按钮在两个列表之间移动权限。
Unfortunately, I cannot get this to work properly. 不幸的是,我无法使它正常工作。
If I populate the parent list in Page_Load
, the selected index seems to reset. 如果我在
Page_Load
填充父级列表,则所选索引似乎会重置。 I used ViewState
to save the index... but this seems to require an additional refresh, because it doesn't update after the PostBack. 我使用
ViewState
保存索引...但是这似乎需要额外的刷新,因为它在PostBack之后不会更新。 This is not acceptable. 这是不可接受的。
If I populate the children listboxes on the OnParentSIC
event, there is no way I can keep track of their selected index. 如果我在
OnParentSIC
事件上填充子级列表框,则无法跟踪它们的选定索引。 The OnChildXSIC
events never fire, because the listboxes get repopulated "too soon". OnChildXSIC
事件永远不会触发,因为列表框“很快就会”被填充。 (?) (?)
How can I get this to work as intended? 如何使它按预期工作? Maybe there is a better solution but I'd really like to understand how to get this solution to work, as I can't see a possible solution at the moment.
也许有更好的解决方案,但是我真的很想了解如何使该解决方案生效,因为目前还看不到可行的解决方案。
Control.ascx Control.ascx
<%@ Control Language="C#" AutoEventWireup="true" EnableViewState="True" CodeBehind="..." Inherits="..." %>
<form runat="server">
<asp:ListBox ID="LBParent" runat="server" CssClass="form-control"
AutoPostBack="true" OnSelectedIndexChanged="OnParentSIC" />
<asp:ListBox ID="LBChild1" runat="server" CssClass="form-control"
AutoPostBack="true" OnSelectedIndexChanged="OnChild1SIC" />
<asp:ListBox ID="LBChild2" runat="server" CssClass="form-control"
AutoPostBack="true" OnSelectedIndexChanged="OnChild2SIC" />
</form>
Control.ascx.cs Control.ascx.cs
protected void Page_Load(object sender, EventArgs e)
{
// Populate parent
for(...) LBParent.Items.Add(...);
}
use Like this 使用像这样
If You are on the Same Page then You can Use ViewState
/ Hidden Fields
to Maintain States 如果您在同一页面上,则可以使用
ViewState
/ Hidden Fields
维护状态
First Time Adding ListItem on PageLoad Use Like this 第一次在PageLoad上添加ListItem像这样使用
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
AddListItems();
}
}
Protected Void AddListItems()
{
// Populate parent
for(...) LBParent.Items.Add(...);
for(...) SecondList.Items.Add(...);
for(...) ThirdList.Items.Add(...);
}
The Onchange-event fires, but before that the OnLoad fires. 触发Onchange事件,但在此之前触发OnLoad。 So:
所以:
So I would try to save the current-selected-index before you rebuild the list (in the Onload). 因此,在重建列表(在Onload中)之前,我将尝试保存current-selected-index。
If you restore it later on while you are in the same 'postback' you can save it in a simple variable. 如果稍后在同一“回发”中还原它,则可以将其保存在一个简单变量中。 No need to store in a Viewstate or Session.
无需存储在Viewstate或Session中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.