简体   繁体   中英

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).

I am then attempted to combine all three drop down lists I have (Day Month and Year) into one DateTime variable. 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.

This issues occurs on both the check in and check out values.

Thanks

use IsPostBack Event in page_load

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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