当我使用我的下拉列表,并尝试存储我想要存储的值时。它只存储像默认值(例如,我有一个下拉列表,其中包含从2015年到2020年的年份。如果我选择2018年,它将返回到2015年的原始版本。)
然后我尝试将我拥有的所有三个下拉列表(Day Month和Year)合并到一个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只是一个输出我输入内容的标签,只是因为我忘记了断点。
签入和签出值都会出现此问题。
由于
答案 0 :(得分:1)
在page_load
尝试:
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();
}