Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a listbox that I want to be populated when page loads. The listbox will be populated with names of people. These names are associated each person's email. My method is not working. When page loads the listbox is empty.

When I hover mouse over object staffEmails I can see count = 16. there are 16 names and the emails. When I expand the staffEmails I see [0] {FTACaseReseting.Models.GetRequestorInfoModel}. When I expand this I see DisplayName "Jim, Doe" Email "Jim.Doe@gmail.com". However my listbox is not populated with this name or other names.

I am not sure how to change my method so the listbox is populated with the names. Please help

Here is my method.

What I have tried:

C#
public async void PopulateAdditionalStaffEmailListBox()
        {
            try
            {
                List<GetRequestorInfoModel> staffEmails = new List<GetRequestorInfoModel>();
                staffEmails = await FTACaseReseting.Controllers.RequestorInfoController.GetAllRequestorInfoes();
                staffEmails = staffEmails.OrderBy(x => x.DisplayName).ToList();
                AdditionalStaffEmailListBox.DataSource = staffEmails;

                AdditionalStaffEmailListBox.DataSource = "DisplayName";
                AdditionalStaffEmailListBox.DataValueField = "Email";
                //Clear all selections in the listbox after populating
                AdditionalStaffEmailListBox.ClearSelection();
            }
            catch (Exception ex)
            {
                string errorMsg = string.Format("An error has occured in {0}. \nException:\n{1}", "AdditionalStaffEmailListBox()", ex.Message);
                Response.Write("<script>alert('" + errorMsg + "')</script>");
            }
        }
Posted
Updated 19-Dec-19 5:10am

C#
AdditionalStaffEmailListBox.DataSource = staffEmails;

AdditionalStaffEmailListBox.DataSource = "DisplayName";

That second statement is destroying the data source list.
 
Share this answer
 
You could try to replace
C#
AdditionalStaffEmailListBox.DataSource = "DisplayName";
with
C#
AdditionalStaffEmailListBox.DataTextField = "DisplayName";
 
Share this answer
 
v2
Comments
Member 11403304 19-Dec-19 10:45am    
The only options available for AdditionalStaffEmailListBox. are Dispose, Disposed SupportDisabledAttribute.

When I hover mouse over AdditionalStaffEmailListBox I see AdditionalStaffEmailListBox{System.Web.UI.WebControls.ListBox}
phil.o 19-Dec-19 10:53am    
Please see my updated answer.
Quote:
C#
public async void PopulateAdditionalStaffEmailListBox()
You've found one of the many reasons to avoid async void methods[^]. :)

Your Page_Load event handler calls PopulateAdditionalStaffEmailListBox. That method gets as far as the first await statement and returns.

The calling code has no way of knowing that the method has not completed yet, so it continues to run. The page is rendered and the content is sent back to the user - without any values in the list.

At some later point, the await completes, and the rest of the PopulateAdditionalStaffEmailListBox method runs. But it's too late - the response has already been sent back to the client. Anything you add to the list now will simply be thrown away.

There are a couple of ways to solve this. Both will require that you add Async="True" to your <%@ Page %> directive. And both will require that you change your PopulateAdditionalStaffEmailListBox method to return a Task.

Option 1:
Use the RegisterAsyncTask method[^]:
C#
public async Task PopulateAdditionalStaffEmailListBox() { ... }

public void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        Page.RegisterAsyncTask(new PageAsyncTask(PopulateAdditionalStaffEmailListBox));
    }
}

Option 2:
Use async model binding[^] (requires .NET 4.6 or later):
C#
public async Task<IEnumerable<GetRequestorInfoModel>> GetAdditionalStaffEmails()
{
    var staffEmails = await FTACaseReseting.Controllers.RequestorInfoController.GetAllRequestorInfoes();
    return staffEmails.OrderBy(x => x.DisplayName);
}
Markup:
ASP.NET
<asp:ListBox ID="AdditionalStaffEmailListBox" runat="server"
    SelectMethod="GetAdditionalStaffEmails"
    DataTextField="DisplayName"
    DataValueField="Email"
/>

Using Asynchronous Methods in ASP.NET 4.5 | Microsoft Docs[^]
 
Share this answer
 
v2
Comments
Member 11403304 19-Dec-19 11:24am    
That is a great solution. But before I try it. I want you to see how my page is currently loaded. Here is that code. Maybe that will help you see what I am doing.
private void _Default_Load(object sender, EventArgs e)
{
this.PopulateAdditionalStaffEmailListBox();
}
Richard Deeming 19-Dec-19 11:29am    
Yes, that's exactly as I thought it was. :)
Member 11403304 19-Dec-19 11:43am    
After making the changes you suggested, the AdditionalStaffEmailListBox is populated. However the object is displayed instead of names. This is what is populated FTACaseReseting.Model.GetRequestorInfoModel
Richard Deeming 19-Dec-19 11:45am    
You'll need to set the DataValueField and DataTextField in the markup. I've updated my answer.
Member 11403304 19-Dec-19 12:03pm    
Yeeeah. That worked. Since I am in the process of developing a web application. You will probably see a lot of questions from me. I am curious if there was a way to send you an email with a question/s or post here and you will see it?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900