Unselectable dates in Calendar

N

na

I need to make certain date selectable in a calendar control depending
on records found in a database. For example, if the 10/20/2006 and
11/20/2006 records exist in the database, then only enable and
highlight thoese two days in the calendar control. Also, I need to
disable all weekends. Is this doable? How do I do this in the
calendar control's DataBinding, Load, or PreRender event. Thanks.
 
N

na

Thank you for your response.

Great article, but I need the dates rendered as text inside e.cell, not
as <A> tag for those dates that have not data in the database. I used
e.cell.enabled=false, but it still render each date as <A> inside <TD>
tag. Thanks.
 
M

Mark Rae

Great article, but I need the dates rendered as text inside e.cell, not
as <A> tag for those dates that have not data in the database. I used
e.cell.enabled=false, but it still render each date as <A> inside <TD>
tag. Thanks.

Preventing days being "clickable" is extremely simple. All you do is clear
the controls in the <td> and then write the date back in so that it's still
visible.

E.g. the following code will prevent dates being clicked if they fall on a
weekend:

protected void cal_DayRender(object source, DayRenderEventArgs e)
{
if (e.Day.IsWeekend)
{
e.Cell.Controls.Clear();
e.Cell.Text = e.Day.DayNumberText;
}
}

You could also give the user some sort of visual hint that the weekend dates
are "different", maybe by changing the back colour of the <td> or the font
colour.

You can even get rid of the dates entirely. I sometimes do this if they fall
outside the current month e.g.

if (e.Day.IsOtherMonth)
{
e.Cell.Controls.Clear();
e.Cell.Text = "&nbsp;"
}
 
N

na

Thanks, Mark. Now add one more twist. How do I disable the dates that
does not have record in a database? I hope I dont' have to connect to
the database and query it using the e.date as a parameter. It would be
too many connections and queries going on. Thanks.
 
M

Mark Rae

Thanks, Mark. Now add one more twist. How do I disable the dates that
does not have record in a database? I hope I dont' have to connect to
the database and query it using the e.date as a parameter. It would be
too many connections and queries going on

No of course not - that would be ridiculous.

I do this by querying the database once to return all records between the
start and end date of the calendar (or calendars) on the page, which I then
store in a List<DateTime> generic collection. Then, on the DayRender, I
simply check if the generic list contains the key e.Date.

pseudo-code:

List<DateTime> mlstDates = new List<DateTime>;

Page_Init or Page_Load
{
<clear out mlstDates>;
SqlDataReader objDR = <SqlDataReader of dates>;
while (objDR.Read())
{
mlistDates.Add(<Date from SqlDataReader>);
}
}

Calendar_DayRender
{
if (!mlstDates.ContainsKey(e.Date))
{
// disable the date in the Calendar;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top