Adding / removing <asp:ListBox elements client-side

M

Mark Rae

Hi,

Looking for some advice again...

Imagine two ListBox controls denoting something like students and team
membership e.g. many students can be members of many teams (e.g. the hockey
team, the football team, the athletics team etc).

Team membership is managed on a standard web page with two ListBox
controls - the one on the left shows which team(s) the student is not a
member of, and the one on the right shows the team(s) which the student is a
member of e.g.

<asp:TextBox ID="txtStudent" runat="server" /><br />
<br />
<table>
<tr>
<td><b>Not member of</b></td>
<td><b>Member of</b></td>
</tr>
<tr valign="middle">
<td align="center">
<asp:ListBox ID="lstNotMemberOf" Runat="server" Rows="15"
Width="200px" />
</td>
<td align="center">
<asp:ListBox ID="lstMemberOf" Runat="server" Rows="15"
Width="200px" />
</td>
</tr>
</table>

When the page is first loaded, the textbox is populated with the student's
name, and the two listboxes are populated with the lists of teams of which
that student is not a member / is a member respectively.

In order to minimise postbacks, network traffic and database access, there
are client-side buttons which "toggle" the team membership. I.e, you
highlight one of the elements in one of the lists and hit the button, which
calls a JavaScript function to remove that element from the box it's
currently in to the other box.

All of this is working perfectly. However, the problem lies when the user
has finished making modifications to the student's team membership, and hits
the Save button. This posts back to the server but, of course, the updated
collection of elements in the two ListBoxes is not persisted across the
postback.

To get round this, I'm using what seems to be the "standard" workaround for
this problem i.e. two hidden textboxes containing the updated collection of
elements, as these ARE persisted across the postback. See what I mean here:
http://www.velocityreviews.com/foru...t-scripts-not-submitting-with-asplistbox.html

However, I'm wondering if there is a better / more efficient way of
achieving this...?

Any assistance gratefully received.

Mark

P.S. ASP.NET 2 / VS.NET 2005, all the latest patches etc.
 
T

Tim Payne

Obvious question, but are you reloading the lists each time the page is
loaded? If so, that would be resetting your lists. If you are, just put a
check for IsPostBack round the list population code and only run it
IsPostBack is false.

Cheers,

Tim.
 
M

Mark Rae

Obvious question, but are you reloading the lists each time the page is
loaded? If so, that would be resetting your lists. If you are, just put a
check for IsPostBack round the list population code and only run it
IsPostBack is false.

Please re-read the OP...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top