Custom ASP.Net booking calendar that is connected to SQL server (want to make certain days unavailable for booking)

StackOverflow https://stackoverflow.com/questions/23409430

Pergunta

I want to be able to choose lets say three days in my calendar, the startDate and endDate gets stored in my SQL Server database that I have connected through a dataset and server explorer only. I'm not sure if I'm doing it right with the TableAdapter's in the code so please let me know if I am doing it all wrong.

When the booked dates is stored in my database table I want the calendar to load these reserved dates into the calendar (like a source). I want this so I can make those dates unavailable to choose from.

Right now I can store data in the database through the addBookedDatesToDatabase-TableAdapter.

When I debug the DayRender method it seems like the foreach look works also. But the e.Cell.Enabled = false; command doesn't seem to work. Why isn't it working? :S

Database:

  • Table SummerCabin contains: ID, StartDate, EndDate, Customer (another table)
  • Table Customer contains: ID, Name, PartySize

So who doesn't the booked dates cells get inactivated? Thank you for your time, I appreciate it a lot!

The code:

using BlavikBookingSite.Data.DataSet1TableAdapters;
using BlavikBookingSite.Data;

namespace BlavikBookingSite.HTML
{
public partial class Home : System.Web.UI.Page
{
    //get accecc to the table
    DataSet1.SummerCabinDataTable cabinTable = new SummerCabinTableAdapter().GetData();
    SummerCabinTableAdapter addBookedDatesToDatabase = new SummerCabinTableAdapter();

    private List<DateTime> bookedDates = new List<DateTime>();
    private List<DateTime> confirmedBookings = new List<DateTime>();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            bookingCalendar.Visible = false;
        }
    }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        if (bookingCalendar.Visible)
        {
            bookingCalendar.Visible = false;
        }
        else
        {
            bookingCalendar.Visible = true;
        }
    }

    protected void bookingCalendar_SelectionChanged(object sender, EventArgs e)
    {
        if (Session["SelectedDates"] != null)
        {
            List<DateTime> newList = (List<DateTime>)Session["SelectedDates"];
            foreach (DateTime date in newList)
            {
                if (bookingCalendar.SelectedDates.Contains(date) || bookingCalendar.SelectedDate == date)
                {
                    bookingCalendar.SelectedDates.Remove(date);

                }
                else
                {
                    bookingCalendar.SelectedDates.Add(date);

                }
            }
            bookedDates.Clear();
        }
    }

    protected void bookingCalendar_DayRender(object sender, DayRenderEventArgs e)
    {
        // Loop through all rows in the table            
        foreach (DataSet1.SummerCabinRow row in cabinTable.Rows)
        {
            //If its a date between start & endDate (Can you make this shorter somehow?)
            if (e.Day.Date >= row.StartDate.Date && e.Day.Date < row.EndDate.Date || 
                e.Day.Date > row.StartDate.Date && e.Day.Date <= row.EndDate.Date)
            {
//[-------------This line below doesent work for some reason?------------------
                e.Cell.Enabled = false;
            }
        }
        if (e.Day.IsSelected)
        {
            bookedDates.Add(e.Day.Date);

        }
        Session["SelectedDates"] = bookedDates;
    }

    protected void book_btn_Click(object sender, EventArgs e)
    {
        List<DateTime> newList = (List<DateTime>)Session["SelectedDates"];
        foreach (DateTime date in newList)
        {
            calendarDateSelected.Text += date.ToShortDateString() + "<br/>";
            addBookedDatesToDatabase.Insert(newList[0], newList[newList.Count - 1], 1);
        }
    }
}
Foi útil?

Solução

e.Cell is an instance of TableCell control, which cannot be enabled or disabled despite having Enabled property. If you want a particular date to be unavailable for user to select - use e.CalendarDay.IsSelectable:

e.CalendarDay.IsSelectable = false;
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top