简体   繁体   English

ASP.NET填充在选定索引更改时互相引用的列表

[英]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控件: LBParentLBChild1LBChild2

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选择一个值时,我想以编程方式用一些数据填充LBChild1LBChild2 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: 所以:

  1. The user clicks on an selection 用户单击一个选择
  2. A postback is triggered 触发回发
  3. At the server the Onload fires (and rebuilds the list) 在服务器上,Onload触发(并重建列表)
  4. The OnSelectedIndexChanged fires (which lost the selection by now) 触发OnSelectedIndexChanged(目前丢失选择)

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.

相关问题 ASP.NET C#具有回发功能的两个下拉列表使彼此的索引混乱 - ASP.NET C# Two DropDown Lists with postback keep messing up each other's Index 下拉列表中的jQuery blockUI选择索引更改asp.net - jQuery blockUI on dropdown selected index change asp.net 所选索引中网格中文本框的Asp.Net更改值已更改 - Asp.Net change value of textbox in grid on selected Index changed asp.net中下拉列表的选定索引更改事件 - Selected Index change event of drop down list in asp.net 如何在ASP.net MVC中的组合框所选项目上填充明细字段更改 - How to populate details fields on combo box selected item change in ASP.net MVC Asp.Net C#DropDownList选定的索引更改大约需要50到60秒 - Asp.Net C# DropDownList Selected Index Change Taking about 50 to 60 Sec 如何使用asp.net中的另一个下拉列表更改下拉列表选择的索引和可见性? - How to change drop down list selected index and visibility using another drop down list in asp.net? 选定索引更改下拉列表 asp.net 更新面板后 Jquery 数据表丢失 - Jquery Datatable lost after selected index change dropdownlist asp.net update panel 在ASP.NET中选择输入类型的所选索引更改上调用javascript函数 - calling javascript function on selected index change of select input type in ASP.NET ASP.net MVC中的Enum下拉列表中的选定项目 - Selected Item in Dropdown Lists from Enum in ASP.net MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM