简体   繁体   English

C#ASP.NET-下拉列表存储当前值

[英]C# ASP.NET - Drop Down Lists storing current value

When I'm using my drop down list, and trying to store the value of which i'm wanting to store. 当我使用下拉列表并尝试存储我要存储的值时。 It is only storing like the default value (So for example, I have a drop down list with the years from 2015 to 2020. If I choose 2018, it'll pass back to the original of 2015). 它只像默认值那样存储(例如,我有一个下拉列表,其中包含从2015年到2020年的年份。如果选择2018年,它将返回到2015年的原始值)。

I am then attempted to combine all three drop down lists I have (Day Month and Year) into one DateTime variable. 然后,我尝试将我拥有的所有三个下拉列表(日月和年)合并到一个DateTime变量中。 However everything then just goes back to default. 但是,然后一切都恢复为默认设置。

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

Is anyone able to aid in me attempting to fix this? 有人可以帮助我解决此问题吗? testing.Text is simply a label outputting what I entered, just incase I forgot to put a breakpoint. testing.Text只是输出我输入内容的标签,以防万一我忘记放置断点。

This issues occurs on both the check in and check out values. 签入和签出值均会出现此问题。

Thanks 谢谢

use IsPostBack Event in page_load page_load使用IsPostBack事件

try: 尝试:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM