簡體   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