[英]C# Asp.net Cascading drop-down select lists using Searchable Options List plugin
[英]C# ASP.NET - Drop Down Lists storing current value
當我使用下拉列表並嘗試存儲我要存儲的值時。 它只像默認值那樣存儲(例如,我有一個下拉列表,其中包含從2015年到2020年的年份。如果選擇2018年,它將返回到2015年的原始值)。
然后,我嘗試將我擁有的所有三個下拉列表(日月和年)合並到一個DateTime變量中。 但是,然后一切都恢復為默認設置。
protected void Page_Load(object sender, EventArgs e)
{
int[] days = new int[31];
for (int i = 0; i < days.Length; i++)
{
days[i] = i + 1;
}
//Binding the information to drop downlist.
ddlDayCI.DataSource = days;
ddlDayCI.DataBind();
ddlDayCO.DataSource = days;
ddlDayCO.DataBind();
int[] months = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
ddlMonthCI.DataSource = months;
ddlMonthCI.DataBind();
ddlMonthCO.DataSource = months;
ddlMonthCO.DataBind();
int[] years = new int[] { 2015, 2016, 2017, 2018, 2019, 2020 };
ddlYearCI.DataSource = years;
ddlYearCI.DataBind();
ddlYearCO.DataSource = years;
ddlYearCO.DataBind();
}
protected void CheckAvailability_Click(object sender, EventArgs e)
{
int yearCI = Convert.ToInt32(ddlYearCI.SelectedItem.Value);
int monthCI = Convert.ToInt32(ddlMonthCI.SelectedItem.Value);
int dayCI = Convert.ToInt32(ddlDayCI.SelectedItem.Value);
DateTime dateOfCheckIn = new DateTime(yearCI, monthCI, dayCI);
int yearCO = Convert.ToInt32(ddlYearCO.SelectedItem.ToString());
int monthCO = Convert.ToInt32(ddlMonthCO.SelectedItem.ToString());
int dayCO = Convert.ToInt32(ddlDayCO.SelectedItem.ToString());
DateTime dateOfCheckOut = new DateTime(yearCO, monthCO, dayCO);
testing.Text = dateOfCheckIn.ToString();
}
有人可以幫助我解決此問題嗎? testing.Text只是輸出我輸入內容的標簽,以防萬一我忘記放置斷點。
簽入和簽出值均會出現此問題。
謝謝
在page_load
使用IsPostBack事件
嘗試:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
FillDropDowns();
}
}
protected void FillDropDowns()
{
int[] days = new int[31];
for (int i = 0; i < days.Length; i++)
{
days[i] = i + 1;
}
//Binding the information to drop downlist.
ddlDayCI.DataSource = days;
ddlDayCI.DataBind();
ddlDayCO.DataSource = days;
ddlDayCO.DataBind();
int[] months = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
ddlMonthCI.DataSource = months;
ddlMonthCI.DataBind();
ddlMonthCO.DataSource = months;
ddlMonthCO.DataBind();
int[] years = new int[] { 2015, 2016, 2017, 2018, 2019, 2020 };
ddlYearCI.DataSource = years;
ddlYearCI.DataBind();
ddlYearCO.DataSource = years;
ddlYearCO.DataBind();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.