c# - ASP.NET populate lists referencing each other on selected index change -
i have asp.net command 3 listboxes: lbparent
, lbchild1
, lbchild2
.
lbparent
has filled programmatically. in page_load
.
when user selects value in lbparent
, want fill both lbchild1
, lbchild2
data, programmatically. children lists must maintain track of selected value.
basically, parent list list of users, first list list of permissions user has, sec list list of permissions user doesn't have.
my plan add together 2 buttons move permissions between 2 lists.
unfortunately, cannot work properly.
if populate parent list in page_load
, selected index seems reset. used viewstate
save index... seems require additional refresh, because doesn't update after postback. not acceptable.
if populate children listboxes on onparentsic
event, there no way can maintain track of selected index. onchildxsic
events never fire, because listboxes repopulated "too soon". (?)
how can work intended? maybe there improve solution i'd understand how solution work, can't see possible solution @ moment.
control.ascx
<%@ command 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(...); }
the onchange-event fires, before onload fires. so:
the user clicks on selection a postback triggered at server onload fires (and rebuilds list) the onselectedindexchanged fires (which lost selection now)so seek save current-selected-index before rebuild list (in onload).
if restore later on while in same 'postback' can save in simple variable. no need store in viewstate or session.
c# asp.net listbox postback autopostback
No comments:
Post a Comment