2013-10-08 2 views
0

Je souhaite mettre à jour les entrées d'une liste déroulante lorsque je modifie l'élément sélectionné dans l'autre liste déroulante. J'ai trois listes déroulantes qui représentent les jours, mois et année ce que je veux, c'est que lorsque je mets à jour le mois je veux que les jours dans la liste déroulante des jours changent en conséquence, par exemple je sélectionne 4 (ie avril) dans le mois. jours pour afficher les entrées LDD jusqu'à 30. Voici ce que je l'ai fait, mais il n'a pas d'effetmettre à jour les éléments de la liste déroulante en les sélectionnant dans une autre liste déroulante

if (ddlmonth.SelectedIndex.Equals(6) || ddlmonth.SelectedIndex.Equals(6) || ddlmonth.SelectedIndex.Equals(9) || ddlmonth.SelectedIndex.Equals(11)) 
{ 

    for(int i = 1; i <= 30; i++) 
    { 
     this.ddldate.Items.Add(i.ToString()); 
    } 
} 

J'ai activé auto postback pour les 3 DDL est encore il n'y a pas d'effet. ET sur Page_Load j'ai ajouté les années et les mois suivants pour être disponibles pour la sélection.

if (!IsPostBack) 
{ 
    for (int i = 1990; i < 2021; i++) 
     this.ddlyear.Items.Add(i.ToString()); 

    for (int i = 1; i <= 12; i++) 
     this.ddlmonth.Items.Add(i.ToString()); 

} 
+0

Comment connectez-vous des listes déroulantes aux gestionnaires d'événements? –

+0

Pourriez-vous clarifier ce que cela veut dire "Voici ce que j'ai fait mais cela n'a aucun effet"? Aussi, veuillez noter que vous doublez les deux premières expressions dans le if (ddlmonth.SelectedIndex.Equals (6)) – Alezis

+0

J'ai écrit le premier morceau de code dans l'événement SelectedIndexChanged du mois DDL de sorte que lorsque je change l'index du mois DDL le les éléments de la date DDL changent en conséquence, c'est-à-dire 30 jours ou 31 jours, etc. pour les mois respectifs et le deuxième morceau de code est écrit dans l'événement Page Load. Merci de souligner les doubles expressions. – Hassan

Répondre

0

Voici quelques trucs code de test

Aspx code de la page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Temp._Default" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server">Year 
    <asp:DropDownList ID="_year" runat="server" onselectedindexchanged="_year_SelectedIndexChanged" AutoPostBack="true"> 
    </asp:DropDownList>Month 
    <asp:DropDownList ID="_month" runat="server" onselectedindexchanged="_month_SelectedIndexChanged" AutoPostBack="true"> 
    </asp:DropDownList>Day 
    <asp:DropDownList ID="_day" runat="server" onselectedindexchanged="_day_SelectedIndexChanged" AutoPostBack="true"> 
    </asp:DropDownList> 
    </form> 
</body> 
</html> 

et ASPX. le code de fichier cs était

namespace Temp 
{ 
    public partial class _Default : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      if (!Page.IsPostBack) 
      { 
       _year.DataSource = GetData.getYear(); 
       _year.DataTextField = "Year"; 
       _year.DataBind(); 
       _month.Items.Add(new ListItem("Select")); 
       _month.DataBind(); 
       _day.Items.Add(new ListItem("Select")); 
       _day.DataBind(); 
      } 
     } 
     protected void _year_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      _month.Items.Clear(); 
      _month.DataSource = GetData.getMonth(); 
      _month.DataTextField = "MonthName"; 
      _month.DataValueField = "MonthId"; 
      _month.DataBind(); 
     } 
     protected void _month_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      _day.Items.Clear(); 
      _day.DataSource = GetData.getDays(_month.SelectedIndex+1,GetData.isLeapYear(Convert.ToInt32(_year.SelectedValue))); 
      _day.DataTextField = "Day"; 
      _day.DataBind(); 
     } 
     protected void _day_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      //Use selected value of _month _year and _day here 
     } 
    }  
    public class GetData 
    { 
     public static List<day> getDays(int monthId,bool isLeapYear) 
     { 
      int maxLimit; 
      if (monthId == 2) 
      { 
       if (isLeapYear) maxLimit = 29; 
       else maxLimit = 28; 
      } 
      else if (monthId == 1 || monthId == 3 || monthId == 5 || monthId == 7 || monthId == 8 || monthId == 10 || monthId == 12) 
       maxLimit = 31; 
      else maxLimit = 30; 
      List<day> days = new List<day>(); 
      for (int i = 1; i <= maxLimit; i++) 
      { 
       days.Add(new day { Day = i }); 
      } 
      return days; 

     } 
     public static List<year> getYear() 
     { 
      //Set here min and max range of year 
      int minLimit = 1950; 
      int maxLimit = DateTime.Now.Year; 
      List<year> years = new List<year>(); 
      for (int i = minLimit; i <= maxLimit; i++) 
      { 
       //You can modify code for bind year dropdown 
       //respectively change in date or month 
       /*if (isLeapYear) 
       { 
        if(i%4==0) 
         years.Add(new year { Year = i }); 
       } 
       else*/ 
       years.Add(new year { Year = i }); 
      } 
      return years; 
     } 
     public static List<month> getMonth() 
     { 
      List<month> months=new List<month>();    
      months.Add(new month { MonthId = 1, MonthName = "Jan" }); 
      months.Add(new month { MonthId = 2, MonthName = "Frb" }); months.Add(new month { MonthId = 3, MonthName = "Mar" }); 
      months.Add(new month { MonthId = 4, MonthName = "Apr" }); months.Add(new month { MonthId = 5, MonthName = "May" }); 
      months.Add(new month { MonthId = 6, MonthName = "Jun" }); months.Add(new month { MonthId = 7, MonthName = "July" }); 
      months.Add(new month { MonthId = 8, MonthName = "Aug" }); 
      months.Add(new month { MonthId = 9, MonthName = "Sep" }); months.Add(new month { MonthId = 10, MonthName = "Oct" }); 
      months.Add(new month { MonthId = 11, MonthName = "Nov" });months.Add(new month { MonthId = 12, MonthName = "Dec" }); 
      return months; 
     } 
     internal static bool isLeapYear(int p) 
     { 
      if (p % 4 == 0) 
       return true; 
      else return false; 
     } 
    } 
    public class month 
    { 
     public string MonthName { get; set; } 
     public int MonthId { get; set; } 
    } 
    public class day 
    { 
     public int Day { get; set; } 
    } 
    public class year 
    { 
     public int Year { get; set; } 
    } 
} 
+0

@Hassan Je vous suggère d'utiliser ce genre de calendrier avec javascript ou jQuery. – Sam

+0

merci beaucoup @Sam – Hassan

Questions connexes