Click here to Skip to main content
13,900,000 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
I have a drop down list with names of countries and a list box with different types of season like summer, winter etc In ASP.Net and both of them gets updated from a table in my database in the page_load event. During the Page_load event my drop down list and list box gets updated with all the available countries and seasons present in the table in the database. My list box changes dynamically after the country is selected in my drop down list. For example, if I choose USA, then season would change dynamically like fall, winter, summer. Right now it works perfectly. The only thing I want is to add an "ALL" option at the top of my list box if a country is selected which has all the seasons. For example if I choose country "X" in my drop down list and it has all the weathers present in my database, the list box would have an "ALL" option at the top. Basically the "ALL" option would pop up only when all the seasons during the page_load event matches the seasons after a country is selected.

What I have tried:

I tried to get the total count of the list box after my page_loaded for the first time like this
protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                 
                int countListBox = lb.Items.Count;  
            }

        }


protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
        {
                       
            if (lb.Items.Count == countListBox)
            {
                lb.Items.Insert(0, "All");
            }
        }
Posted
Updated 20-Jul-18 20:15pm
Comments
Gerry Schmitz 19-Jul-18 15:09pm
   
I keep reading, and still don't "get it". What is the point of "All". Every country has "4 seasons"; whether it snows or not.

If a "season" means something else, then just add some extra (bool) columns to the "list" to indicate "x season is present" (or whatever).
Member 13863605 19-Jul-18 15:19pm
   
I want to add "All". That's a requirement. Doesn't matter whether every country has four seasons or not. We'll a lot of Asian countries have six seasons so all countries DOES NOT necessarily have four season.
Vincent Maverick Durano 19-Jul-18 20:39pm
   
But how do you determine if a country has all seasons associated? Do you have something like a flag in the database to determine that?

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Echoing what vincent said. Unless you want a gigantic if/switch statement you are going to need a table of countries and a flag on that table, lets call it, HasAllSeasons.

Then in your selected index change event, you'll need to know what country has been selected, query the database to decide if that country has all seasons, then append "All" to your list.

Pseudo code.

protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
        {
            var countryName = "FRANCE";
            var countryQuery = SELECT TOP 1 FROM Countries WHERE CountryName = countryName;
            var hasAllSeasons = countryQuery.FirstOrDefault().HasAllSeasons;

//  lb.Items.Count == countListBox Im not sure the purpose of this bit so i've taken it out.
            if (hasAllSeasons)
            {
                lb.Items.Insert(0, "All");
            }
        }


To me this is the easiest route to go, otherwise you'll need to change out DB query for a switch statement.


protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
        {
            var countryName = "FRANCE";
            var hasAllSeasons = false;

            switch(countryName)
            {
              case "FRANCE": 
              case "GERMANY": 
              case "RUSSIA": 
              case "CHINA": 
              case "JAPAN": 
               hasAllSeasons = true;
               break;
              case "ENGLAND":
              case "VIETNAME":
              case "SOUTH KOREA":
              case "CAMBODIA":
               hasAllSeasons= false;
               break;
            }
            
//  lb.Items.Count == countListBox Im not sure the purpose of this bit so i've taken it out.
            if (hasAllSeasons)
            {
                lb.Items.Insert(0, "All");
            }
        }


As you can see, the switch statement approach is hard coded and can get pretty lengthy, I personally would go the DB route.

Regardless of your approach, you are going to have to define somewhere what countries have all seasons (i don't see you indicating that is done in your question) otherwise there is no logic to determine what countries have all seasons and what countries do not have all seasons.
   
Comments
Vincent Maverick Durano 23-Jul-18 0:04am
   
well said. 5ed!

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web02 | 2.8.190306.1 | Last Updated 21 Jul 2018
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100