繁体   English   中英

DropDownLists,在“选定索引已更改”上,仅在按下另一个按钮时运行-C#

[英]DropDownLists, On Selected Index Changed only runs when another button pressed - C#

所以本质上,我想让我的DropDownList更新分配给它的数字,具体取决于它的月份。 (因此,例如,如果有人选择“二月”,它将更新“日期下拉列表”以限制那里显示的数字量。)

但是,我的问题是,只要运行代码,就不会在更改月份后更新。 从我的断点来看,只有在单击页面上的另一个按钮时才运行, 然后运行thats事件。 这是一个奇怪的问题,我一直在摆弄它无济于事。

我的HTML下拉列表:

<asp:DropDownList ID="ddlMonthCI" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlMonthCI_SelectedIndexChanged"/>

通过尝试EnableViewState="true"和其他一些小事情来解决这个问题,但是没有任何效果。

但是,单击此按钮后,代码将运行:

<asp:Button runat="server" ID="CheckAvailability" Text="CHECK" OnClick="CheckAvailability_Click" CssClass="tsc_c3b_white tsc_button" />

我当前的C#代码

 protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        FillDropDowns();
        ddlMonthCI_SelectedIndexChanged(sender, e);
    }
}

protected void FillDropDowns()
{
    //A new int array created to hold 31 numbers
    var x = System.DateTime.Now;
    //For every 'i' (day), starting at one, and less than 31, incremement.
    for (int i = 1; i < 32; i++)
    {
        ddlDayCI.Items.Add(i.ToString());
        ddlDayCO.Items.Add(i.ToString());
    }
    ddlDayCI.Items.FindByValue(x.Day.ToString()).Selected = true;
    ddlDayCO.Items.FindByValue(x.Day.ToString()).Selected = true;

    //A new array created to hold months of the year
    for (int i = 1; i < 13; i++)
    {
        ddlMonthCI.Items.Add(i.ToString());
        ddlMonthCO.Items.Add(i.ToString());
    }
    ddlMonthCI.Items.FindByValue(x.Month.ToString()).Selected = true;
    ddlMonthCO.Items.FindByValue(x.Month.ToString()).Selected = true;

    for (int i = x.Year; i <= (x.Year + 1); i++)
    {
        ddlYearCI.Items.Add(i.ToString());
        ddlYearCO.Items.Add(i.ToString());
    }
}

protected void ddlMonthCI_SelectedIndexChanged(object sender, EventArgs e)
{
    DateTime sysdate = DateTime.Now;
    int selectedYear = Convert.ToInt32(ddlYearCI.SelectedItem.Text);
    int selectedMonth = Convert.ToInt32(ddlMonthCI.SelectedItem.Text);
    int totaldaysinthismonth = System.DateTime.DaysInMonth(selectedYear, selectedMonth);

    ddlDayCI.Items.Clear();

    for (int i = 1; i <= totaldaysinthismonth; i++)
    {
        ddlDayCI.Items.Add(i.ToString());
    }
}

我错过了有关检查可用性的onClick方法,因为我认为这不相关。 但是,如果您希望我发布它,我会的。

在页面加载中,FillDropDowns(); 方法已运行,但是ddlMonthCI_SelectedIndexChanged也已运行。 我把它放在那里,因为它只有在多数民众赞成运行时才运行。

有任何想法吗?

按要求 -

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

    //If checking out is before checking in date
    if (dateOfCheckOut < dateOfCheckIn)
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(),"Scripts","<script>alert('INCORRECT DATE FORMAT');</script>" );            
    }
    //If checking out is after checking in date
    else if (dateOfCheckOut > dateOfCheckIn)
    {
        //Finds the current systems date
        DateTime sysdate = DateTime.Now;
        //If the current system date is after the check in, or its after the check out
        if (sysdate > dateOfCheckIn || sysdate > dateOfCheckOut)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "Scripts", "<script>alert('Please book AFTER todays date');</script>");
        }
        //else it'll run this
        else
        {
            DLDbContext context = new DLDbContext();
            //Counts how many versions of that room exists
            var roomType = ddlRoomType.SelectedItem.ToString();
            int RoomTypes = (from u in context.Room where u.roomType == roomType select u).Count();
            //Counts how many booked rooms there is, with the same room type.
            int BookedRooms = (from b in context.Booking where b.arrivalDate >= dateOfCheckIn && b.departureDate <= dateOfCheckOut && b.RoomType == roomType select b).Count();

            //If there's less booked rooms, than the amount of Rooms of that type it'll run this
            if (BookedRooms < RoomTypes)
            {

                //Passes current dates of checking in, checking out and roomtype to booking page
                Session.Add("checkIn", dateOfCheckIn);
                Session.Add("checkOut", dateOfCheckOut);
                Session.Add("roomType", roomType);
                Response.Redirect("BookingOverview.aspx");
            }
            //Else run this
            else
            {
                //Unavailable
                Page.ClientScript.RegisterStartupScript(this.GetType(), "Scripts", "<script>alert('Date is unavailable');</script>");
            }
        }
    }
}

我发现了我的错误。 我运行了一个测试站点,以确认这不是我的愚蠢(是对的)。

错误来自我正在使用的UI模板,其中包括

<form id="reservation-form">
   <form runat="server">
<!-- Stuff in here -->
</form>
</form>

导致此问题的原因是第一种形式,我不知道为什么,我也不能给出第二种形式,即ID无法继续CSS类,因为它无法正常工作。

但是,将其删除后,下拉列表可以正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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