ASP.NET w/ VB.NET - MultiSelect on ListBox Or CheckBoxList w/ AutoPostback enabled
Background: I have a winForm app that registers a user in the database based on the user input provided in the form, auto-generates a random password and username for the user, and e-mails the user a link to take an application based on the marketing company selected.
Problem: I got the bundles listbox to populate w/ autopostback set to true but the bundles listbox populates as soon as you click on an lbcarrier and it doesn't allow you select more than one carrier.
Do you have any ideas on how to allow multiselect with the postback feature on?
Here's a screenshot of the interface:
code on default.aspx:
<td class="style1">
Carriers:</td>
<td bgcolor="#ffffff" class="style2">
<asp:ListBox AutoPostback="true" ID="lbCarriers" runat="server" Height="86px" Width="250px">
</asp:ListBox>
</td>
</tr>
<td class="style1">
Bundles:</td>
<td bgcolor="#ffffff" class="style2">
<asp:ListBox ID="bundles" runat="server" Height="86px" Width="250px">
</asp:ListBox>
</td>
</tr>
code on default.aspx.vb:
Protected 开发者_JAVA百科Sub lbCarriers_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles lbCarriers.SelectedIndexChanged
Dim splt() As String
Dim ac1 As Array
bundles.Items.Clear()
Dim item As ListItem = lbCarriers.SelectedItem
splt = item.ToString().Split("|")
ac1 = proxy.GetContractingBundles("test", "test", Trim(splt(0)))
For Each Pitem In ac1
bundles.Items.Add(Trim(splt(2)) & " | " & Pitem.FormBundleName)
Next
End Sub
Thanks for looking!
By definition the AutoPostBack
property will automatically cause a postback to occur when the user changes the list selection.
To enable multiple selections you'll need to turn AutoPostBack
off and enable the SelectionMode
property:
<asp:ListBox SelectionMode="Multiple" ID="lbCarriers" runat="server"
Height="86px" Width="250px">
Note that AutoPostBack
is false
by default so I simply omitted it.
Once the user submits you can process the selected list box items with logic similar to what you have in the lbCarriers_SelectedIndexChanged
event. You can then loop through the items and check the item's Selected
property or loop through the results of the GetSelectedIndices
method and reference the items by their indices.
If that's not the route you want to take, and you want it to be handled on the fly without a postback, then you'll need to write some JavaScript.
EDIT: the code to go through your selected items would be similar to the code below, and you would probably place it in a method that is called by the submitted button's event handler.
bundles.Items.Clear()
For Each item As ListItem In lbCarriers.Items
If item.Selected Then
Dim splt() As String
Dim ac1 As Array
splt = item.ToString().Split("|")
ac1 = proxy.GetContractingBundles("test", "test", Trim(splt(0)))
For Each Pitem In ac1
bundles.Items.Add(Trim(splt(2)) & " | " & Pitem.FormBundleName)
Next
End If
Next
Easiest fix would be to turn AutoPostBack
off, and change the SelectionMode
as suggested. Then have a button, Get Bundles
. In that click event you can add your code to retrieve the bundles based off of the Carrier listbox.
精彩评论