DropDownLists,On Selected Index Changed 仅在按下另一个按钮时运行 - C#
DropDownLists, On Selected Index Changed only runs when another button pressed - C#
所以基本上,我想让我的 DropDownList 更新分配给它的数字,具体取决于它是几月。 (例如,如果有人选择二月,它会更新日期下拉列表以限制那里显示的数字数量。)
但是,我的问题是,只要代码是 运行,它就不会在更改月份时更新。从我的断点来看,只有 运行 当页面上的另一个按钮被单击时,THEN 事件才会 运行。这是一个奇怪的问题,我一直在摆弄它无济于事。
我的 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 方法,因为我认为它不相关。但是,如果您希望我 post 我会的。
在页面加载中,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 class,因为它没有无法正常工作。
但是,删除它后,下拉列表可以正常工作。
所以基本上,我想让我的 DropDownList 更新分配给它的数字,具体取决于它是几月。 (例如,如果有人选择二月,它会更新日期下拉列表以限制那里显示的数字数量。)
但是,我的问题是,只要代码是 运行,它就不会在更改月份时更新。从我的断点来看,只有 运行 当页面上的另一个按钮被单击时,THEN 事件才会 运行。这是一个奇怪的问题,我一直在摆弄它无济于事。
我的 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 方法,因为我认为它不相关。但是,如果您希望我 post 我会的。
在页面加载中,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 class,因为它没有无法正常工作。
但是,删除它后,下拉列表可以正常工作。