15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

1172 ❘ ChaPTer 40 cOre Asp.net<br />

Customizing the Calendar Control<br />

Before adding events to the database, you need to modify your calendar display. It would be nice to display all<br />

days where a booking has previously been made in a different color, <strong>and</strong> prevent such days from being selectable.<br />

This requires that you modify the way you set dates in the calendar <strong>and</strong> the way day cells are displayed.<br />

You will start with date selection. You need to check for dates where events are booked in three places, <strong>and</strong><br />

modify the selection accordingly: 1) when you set the initial date in Page_Load(), 2) when the user attempts<br />

to select a date from the calendar, <strong>and</strong> 3) when an event is booked <strong>and</strong> you want to set a new date to prevent<br />

the user from booking two events on the same day before selecting a new date. Because this is going to be a<br />

common feature, you might as well create a private method to perform this calculation. This method should<br />

accept a trial date as a parameter <strong>and</strong> return the date to use, which will either be the same date as the trial<br />

date, or the next available day after the trial date.<br />

Before adding this method, you need to give your code access to data in the Events table. You can use the<br />

MRBEventData control to do this because this control is capable of populating a DataView. To facilitate this,<br />

add the following private member <strong>and</strong> property (you may also require a namespace import for System.Data<br />

for this code to work):<br />

private DataView eventData;<br />

private DataView EventData<br />

{<br />

get<br />

{<br />

if (eventData == null)<br />

{<br />

eventData =<br />

MRBEventData.Select(new DataSourceSelectArguments())<br />

as DataView;<br />

}<br />

return eventData;<br />

}<br />

set<br />

{<br />

eventData = value;<br />

}<br />

}<br />

code snippet PCSWebSite3/Default.aspx.cs<br />

The EventData property populates the eventData member with data as it is required, with the results<br />

cached for subsequent use. Here you use the SqlDataSource.Select() method to obtain a DataView.<br />

Next, add this method, GetFreeDate(), to the code-behind file:<br />

private DateTime GetFreeDate(DateTime trialDate)<br />

{<br />

if (EventData.Count > 0)<br />

{<br />

DateTime testDate;<br />

bool trialDateOK = false;<br />

while (!trialDateOK)<br />

{<br />

trialDateOK = true;<br />

foreach (DataRowView testRow in EventData)<br />

{<br />

testDate = (DateTime)testRow["EventDate"];<br />

if (testDate.Date == trialDate.Date)<br />

{<br />

trialDateOK = false;<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!