ASP.NET VB.NET - Winforms to Web - implementing LostFocus listview control equivalent for web

1k Views Asked by At

Background: I have a winForm app that registers a user in the database based on the information provided, auto-generates a random password and username, and e-mails the user a link to take an application based on the marketing company selected.

Problem:

  • When the user selects the lbCarrier(s), the Bundles don't show up in the listbox b/c the lostfocus feature doesn't work for asp.net. What code can I use to auto-populate the Bundles listbox based on what is selected in lbCarrier listbox for ASP.NET.

web app screenshot

Code from default.aspx.vb:

Private Sub lbCarriers_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbCarriers.LostFocus
    Dim splt() As String
    Dim ac1 As Array
    bundles.Items.Clear()
    For Each item In lbCarriers.Items

        splt = Split(item.text, "|")
        ac1 = proxy.GetContractingBundles("test", "test", Trim(splt(0)))
        For Each Pitem In ac1
            bundles.Items.Add(Trim(splt(2)) & " | " & Pitem.FormBundleName)
        Next
    Next
End Sub

Protected Sub lbCarriers_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles lbCarriers.SelectedIndexChanged

End Sub
3

There are 3 best solutions below

2
On BEST ANSWER

You'll need to do some client-side javascript, or add an AutoPostBack on the dropdownlist and code the OnSelectedIndexChanged event:

<asp:DropDownList AutoPostBack="true" OnSelectedIndexChanged="MethodName" ...

Also, the LostFocus event only fires on the client, not on the server, and it is a Javascript event called "Blur":

http://www.java2s.com/Code/JavaScriptReference/Javascript-Methods/blur.htm

3
On

I would suggest you look into using jQuery. It makes client side programming much simpler. What you need to do is make an asynchronous AJAX request to the server when the focus is lost, then have the server return just the data you want to display in the list box.

This should go at the bottom of your main page:

<script type="text/javascript">
$(document).ready(function() {
    $("#lbCarrier").onblur(function () {
        // Ask the server for the list
        $.ajax({
           type: "POST",
           url: "getlist.aspx",
           data: "option=" + $("#lbCarrier").val(),
           success: function(result) {
               // When you get the result, populate the Bundles list
               $("#lbBundles").......
           }
         });
    });
});
</script>

You'll need to create a second page (getlist.aspx) that accepts a query string argument of "Option" that returns the bundles for the selected carrier.

0
On

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?

code on default.aspx:

            <asp:ListBox AutoPostback="true" ID="lbCarriers" runat="server" Height="86px" Width="250px">
            </asp:ListBox>

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