Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am not sure how to change my method so that it populates RequestorDropDownList when the page loads.

A user will only be able to select one name from the the RequestorDropDownList.

Please help.

What I have tried:

Here is my method.
C#
public async void RequestorDropDownList()
        {
            List<GetRequestorInfoModel> requestors = new List<GetRequestorInfoModel>();
            RequestorDropDownList.Items.Clear();
            RequestorUpdateDropDownList.Items.Clear();

            try
            {
                requestors = await FTACaseReset.Controllers.RequestorInfoController.GetAllRequestorInfoes();
                requestors = requestors.OrderBy(x => x.DisplayName).ToList();

                #region Populate RequestorDropDownList
                DropDownListItem firstRequestor = new DropDownListItem();
                firstRequestor.Text = "<-Please select Requestor->";
                firstRequestor.Value = 0;
                RequestorDropDownList.Items.Add(firstRequestor);

                for (int i = 0; i < requestors.Count; i++)
                {
                    DropDownItem item = new DropDownItem();
                    item.Text = requestors[i].DisplayName;
                    item.Value = requestors[i].RequestorInfoID;
                    RequestorDropDownList.Items.Add(item);
                }

                if (RequestorDropDownList.Items.Count > 0)
                    RequestorDropDownList.SelectedIndex = 0;
                #endregion

                #region Populate RequestorUpdateDropDownList
                for (int i = 0; i < requestors.Count; i++)
                {
                    DropDownItem item = new DropDownItem();
                    item.Text = requestors[i].DisplayName;
                    item.Value = requestors[i].RequestorInfoID;
                    RequestorDropDownList.Items.Add(item);
                }
                #endregion
            }
            catch (Exception ex)
            {
                string errorMsg = string.Format("An error has occured in {0}. \nException:\n{1}", "PopulateRequestorDropDownList()", ex.Message);
                MessageBox.Show(errorMsg, "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Posted
Updated 19-Dec-19 7:31am

1 solution

You're updating two lists, so make your method async Task instead of async void, and use Page.RegisterAsyncTask:
C#
public async Task RequestorDropDownList() { ... }

public void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        Page.RegisterAsyncTask(new PageAsyncTask(RequestorDropDownList));
    }
}
NB: MessageBox.Show won't work in an ASP.NET application. Your code is running on the server, so the message would pop up on the server, where nobody would ever see it.

It might appear to work when you're debugging the application in Visual Studio. But that's only because, in that specific case, the server and the client are the same computer.

NB2: You're currently adding the items to RequestorDropDownList in both loops. I suspect the second loop should be adding them to RequestorUpdateDropDownList.
 
Share this answer
 
v2
Comments
Member 11403304 19-Dec-19 17:01pm    
Once again thank you Richard. I will post more questions about different things. For example how to hide a page and display a page or panel etc.

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