3
Answers

Dropdownlist selected value based on other ddl values

Riddhi Valecha

Riddhi Valecha

10y
1.4k
1
Hi all,
I have a class Functions as follows -
 public class Functions
{
#region MonthsList
  public void AddMonths(DropDownList ddl)
  {
  try
  {

  for (int i = 1; i <= 12; i++)
  {
  ddl.Items.Add(new ListItem(new DateTime(DateTime.Now.Year, i, 1).ToString("MMM"), i.ToString()));
  }
 
  }
  catch (Exception err) { err.Message.ToString(); }
  }
  #endregion

#region YearsList
  public void AddYears(DropDownList ddl)
  {
  try
  {
 
  for (int Year = 2010; Year <= DateTime.Now.Year+7; Year++)
  {
  ListItem li = new ListItem(Year.ToString(), Year.ToString());
  ddl.Items.Add(li); 
  }
  }
  catch (Exception err) { err.Message.ToString(); }
  }
  #endregion
}
-------
Now, in Default.aspx Page, I have
4 Dropdownlist and 1 Radiobutton.
------------
Now, in Default.aspx.cs Page, the logic I want is as follows -
Page_Load Event()
{
 Functions cf = new Functions();
cf.AddMonths(ddl_months_1);
cf.AddYears(ddl_years_1);
}
---------
Now, the user will select the Month and Year of his/her Choice.
Then the radio button will be visible. Till then, it is not visible.
I have 3 options in RadioButton -
1. Same Month
2. Next Month
3. Next To Next Month
---------------
Now,
If the User Selects Jan 2010 (from dropdownlists) and "Same Month" (from radio button)
Then, the other dropdownlists' selected values must be
 ddl_months_2.selectedvalue = Jan
 ddl_years_2.selectedvalue =2010

If the User Selects Jan 2010 (from dropdownlists) and "Next Month" (from radio button)
Then, the other dropdownlists' selected values must be
 ddl_months_2.selectedvalue = Feb
 ddl_years_2.selectedvalue =2010

If the User Selects Jan 2010 (from dropdownlists) and "Next To Next Month" (from radio button)
Then, the other dropdownlists' selected values must be
 ddl_months_2.selectedvalue = Mar
 ddl_years_2.selectedvalue =2010

If the User Selects Dec 2010 (from dropdownlists) and "Same Month" (from radio button)
Then, the other dropdownlists' selected values must be
 ddl_months_2.selectedvalue = Dec
 ddl_years_2.selectedvalue =2010

If the User Selects Dec 2010 (from dropdownlists) and "Next Month" (from radio button)
Then, the other dropdownlists' selected values must be
 ddl_months_2.selectedvalue = Jan
 ddl_years_2.selectedvalue =2011

If the User Selects Dec 2010 (from dropdownlists) and "Next To NExt Month" (from radio button)
Then, the other dropdownlists' selected values must be
 ddl_months_2.selectedvalue = Feb
 ddl_years_2.selectedvalue =2011
----------------------
I am unable to form this logic.
PLease help.
Answers (3)