2013-10-08 122 views
0

我想在更改另一個下拉列表中的選定項目時更新下拉列表的條目。我有三個代表日,月和年的下拉列表,我想要的是,當我更新月份時,我希望天數下拉列表中的日期相應地改變,例如我在月份中選擇4(即四月)DDL我想天DDL顯示的條目,直到30這裏是我做了什麼,但對所有3 DDL仍然沒有效果沒有影響通過從其他下拉列表中選擇來更新下拉列表項目

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()); 
    } 
} 

我已經啓用了自動回傳。並在Page_Load上添加了以下年份和月份供您選擇。

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

你如何將下拉菜單連接到事件處理程序? –

+0

您能否澄清一下這是什麼意思「這是我所做的,但它沒有效果」?還請注意,您在if(ddlmonth.SelectedIndex.Equals(6))中加入前兩個表達式 – Alezis

+0

我在月DDL的SelectedIndexChanged事件中編寫了第一段代碼,以便當我更改月份DDL的索引時,日期DDL中的項目相應地發生變化,即相應月份爲30天或31天等,第二段代碼寫入Page_Load事件中。感謝您指出雙重表達。 – Hassan

回答

0

下面是一些測試代碼的東西

aspx頁面代碼

<%@ 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> 

和ASPX。 cs的文件代碼是

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我建議你用JavaScript或jQuery使用這種壓光機。 – Sam

+0

非常感謝@Sam – Hassan

相關問題