我有一个ASP.NET控件与三个列表框:LBParent
,LBChild1
和LBChild2
。ASP.NET填充列表在选定的索引变化引用对方
LBParent
必须以编程方式填写。我在Page_Load
中这样做。
当用户在LBParent
中选择一个值时,我想以编程方式填充LBChild1
和LBChild2
以及一些数据。儿童名单还必须记录他们选定的价值。
基本上,父列表是用户列表,第一个列表是用户拥有的权限列表,第二个列表是用户没有的权限列表。
我的计划是添加两个按钮来移动这两个列表之间的权限。
不幸的是,我无法让这个工作正常。
如果我在
Page_Load
中填充父列表,所选的索引似乎重置。我使用ViewState
来保存索引...但这似乎需要额外的刷新,因为它在PostBack之后不会更新。这是不可接受的。如果我填充
OnParentSIC
事件上的子列表框,我无法跟踪他们选择的索引。OnChildXSIC
事件永远不会触发,因为列表框会“过早”重新填充。 (?)
我该如何才能达到预期的效果?也许有一个更好的解决方案,但我真的很想了解如何使这个解决方案的工作,因为我目前看不到一个可能的解决方案。
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
protected void Page_Load(object sender, EventArgs e)
{
// Populate parent
for(...) LBParent.Items.Add(...);
}
'我用ViewState保存索引...但这似乎需要额外的刷新'怎么?你可以发布代码: – 2014-11-08 11:46:23
@Ganesh_Devlekar:基本上,在像OnParentSIC这样的SelectedIndexChanged事件中,我简单地称为ViewState [“parentIdx”] = LBParent.SelectedIndex;'。当这个事件被触发时,它会在列表填充后发生,所以它需要额外的页面刷新。如果我将列表人口代码放在'Page_LoadComplete'中,事件根本不会触发。 – 2014-11-08 11:50:10
请粘贴所有代码你已经尝试 – 2014-11-08 12:18:33