Vb.net – Show loading message on SelectedIndexChanged event of drop down list

asp.netvb.net

I'm trying to show message "Loading…" when a user select an item in the dorp down list.

Mark up:

<asp:Label ID="lbl_LoadingMessage" runat="server" ></asp:Label>

<asp:DropDownList ID="ddl_Chapter" runat="server" AutoPostBack="True">
            </asp:DropDownList>

Code behind:

Protected Sub LoadMessage()
        lblLoading.Text = "Loading..."
End Sub


Protected Sub ddl_Chapter_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddl_Chapter.SelectedIndexChanged

        LoadMessage()

        Dim redirectURL As String = "~/chapter.aspx?bid=" & BookId.ToString
        Server.Transfer(redirectURL)

End Sub

The method I'm using above is not working. When I select a new item from the drop down list, it works as expected except the message "Loading…" is not showing at all. Any suggestion or code sample? Thank you.

Best Answer

You will have to do this on the client side using javascript.

At the moment, your dropdown menu is causing a postback. when the drop down menu is changed, the page post backs then the entire page life cycle is run through. When the event ddl_Chapter_SelectedIndexChanged is run, you set the text of the loading label, but you never reload the page (which would have your loading message) - instead you server.transfer to a new page.

If you use jQuery, you could set the labels text value as soon as the dropdown is changed

something like:

$('#the_full_renedered_ID_of_ddl_Chapter').change(function () {
      $('#the_full_renedered_ID_of_lbl_LoadingMessage').html("Loading...")
});
Related Topic