How to populate dropdownlist based on a DataTable

后端 未结 1 1367

I have the following code which populates a ASP.net label with values retrieved from a SP and a CachedTable:

public void UpdateDropDownList()
{
    string strQue         


        
相关标签:
1条回答
  • 2021-01-27 08:08
    Specialty.DataSource = distinctValues
    Specialty.DataBind()
    

    You may need to adjust the DataTextField and DataValueField properties, but I can't tell what those should be.

    Do the same for all three dropdownlists.

    I would also divide this code into separate functions. Maybe GetSpecialties which retrieves all the specialties from the database. GetProviders which builds the query and returns the providers from the cached table. And GetTopics which builds that query and returns the Topics.

    That would make the main function much shorter and easier to understand. As it is right now it is very hard to read.

    var distinctSpecialties = GetSpecialties();
    Specialty.DataSource = distinctSpecialties;
    Specialty.DataBind();
    
    var distinctProviders = GetProviders(distinctSpecialties);
    Name.DataSource = distinctProviders;
    Name.DataBind();
    
    var distinctTopics = GetTopics(distinctSpecialties);
    Topic.DataSource = distinctTopics;
    Topic.DataBind();
    
    0 讨论(0)
提交回复
热议问题