So essentially, I'd like to make my DropDownList update the numbers assigned to it, depending on what month it is. (so for example, If someone chooses February, it would update the Day Drop Down List to limit the amount of numbers shown there.).
However, my issue is that whenever the code is run, it doesn't update upon the month being changed. Judging by my breakpoints, it is only ran when another button that is on the page is being clicked, THEN thats event will run. It's an odd issue and I have been fiddling about with it to no avail.
My HTML for the Drop Down List:
<asp:DropDownList ID="ddlMonthCI" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlMonthCI_SelectedIndexChanged"/>
Fiddled with this a few times by trying EnableViewState="true"
and a few other minor things but nothing has worked.
Upon clicking this button though, the code IS run:
<asp:Button runat="server" ID="CheckAvailability" Text="CHECK" OnClick="CheckAvailability_Click" CssClass="tsc_c3b_white tsc_button" />
My current C# code
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());
}
}
I have missed out the onClick method regarding checking availability because I don't see that relevant. However if you wish me to post it I will.
Within the page load, the FillDropDowns(); method is ran, however so is the ddlMonthCI_SelectedIndexChanged. I placed that in there due to it only being run when thats run.
Any ideas?
By Request -
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>");
}
}
}
}
I found my error. I ran a test site to confirm that it wasn't my stupidity (and was right).
The error came from a UI Template I was using which consisted of
<form id="reservation-form">
<form runat="server">
<!-- Stuff in here -->
</form>
</form>
The thing that was causing the issue was the first form, I have no idea why, I also couldn't give the second form that ID to continue the CSS class as it doesn't work properly.
However, after removing it, the dropdownlists work correctly.