3

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: screenshot

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 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!

Brian McCarthy
  • 4,658
  • 16
  • 49
  • 66
  • I don't think there is an easy way to do this. There is no way for the control to know they are done selecting items. You might put `autopostback=false` and hook up your own javascript code to cause a postback once the textbox loses focus. I think this is less than ideal for a user experience perspective. Alternatively, you could use AJAX to populate the next textbox, but that of course adds a lot of overhead. Lastly, you might be able to have your textbox be a trigger for an update panel that includes your next textbox. – Prescott Apr 12 '11 at 14:22

2 Answers2

3

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
Ahmad Mageed
  • 94,561
  • 19
  • 163
  • 174
  • thanks for your response! Could you please post some example code that enables the SelectionMode property? I'm not clear how to implement this. – Brian McCarthy Apr 12 '11 at 14:47
  • @Brian I've updated my post. You can specify it as part of the markup rather than enabling it in the code behind. – Ahmad Mageed Apr 12 '11 at 14:54
  • thanks for the update. What loop would u add under Protected Sub lbCarriers_SelectedIndexChanged? Please provide code if possible. I already have the loop For Each Pitem In ac1 bundles.Items.Add(Trim(splt(2)) & " | " & Pitem.FormBundleName) – Brian McCarthy Apr 12 '11 at 15:42
1

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.

Jack
  • 8,851
  • 3
  • 21
  • 26