The Calendar Control and The DayRender Event in ASP.NET

xiaoxiao2021-03-06  55

In this article, we will use the Calendar control and DataGrid controls to display a calendar of events. The calendar will display small icons and highlight each day where an event occurs. When the user selects a date, the grid will show additional details for the events of selected day. A screen shot of the running web form is shown below. We will first need a source of data. In this example we will use a DataTable, but you should pick what works best for your application. Our web form simulates A Database Query by populating a datatable programatically. // to Simulate a Database Query

Socialevents = new dataable ();

Socialevents.columns.add (New Datacolumn ("Date", TypeOf (DateTime))

Socialevents.columns.add (New Datacolumn ("Description", TypeOf (String));

Socialevents.columns.add (New Datacolumn ("URL", TypeOf (String));

DataRow Row;

Row = socialevents.newrow ();

Row ["Date"] = DateTime.now.adddays (-5);

Row ["" "] =" Poker Game At Scott's ";

ROW ["URL"] = "http://www.odetocode.com/blogs/scott/";

SocialEvents.Rows.Add (row);

//

// Add more rows ...

//

Unlike the DataGrid, the Calendar control has no built in ability to data bind, but the control does allow us to modify the format and content rendered by catching the DayRender event. The DayRender event fires each time the calendar creates a table cell for the calendar table. The event has an argument of type DayRenderEventArgs. The argument has two properties, one property to represent the date for the cell, and one to represent the TableCell control. Let's take a look at the event handler for the DayRender event. private void Calendar1_dayrender (Object Sender, DayRendreventArgs E) {

DataRow [] rows = socialevents.select (

String.Format

"DATE> = # {0} # and date <# {1} #",

E.day.date.toshortdateString (),

E.day.date.adddays (1) .toshortdateString ()

)

);

Foreach (DataRow Row In Rows)

{

System.Web.ui.WebControls.Image Image;

Image = new system.Web.ui.WebControls.image ();

Image.imageurl = "dot.gif";

Image.tooltip = row ["description"]. TOSTRING ();

E.cell.controls.add (image);

E.Cell.backcolor = color.firebrick;

}

}

Our first step is to find out if there are any events for the current date. In the above code we are using the DataTable Select method to find matching records, but again you'll have to adjust the algorithm to suit your application's performance and scalability requirements. Depending on the size of the data and the number of users you need to support, the Select method may not be the best fit, but it is easy to use for this example. We need to build a string for the Select expression that Will Look Like The Following. "Date> = # 8/24/2004 # and date <# 8/25/2004 #".

ADO.NET expressions require dates enclosed within # characters. Comparing dates can be a tricky problem, particularly when the underlying type, like the .NET DateTime type, includes time information. To truncate the time, we use the ToShortDateString method of the DateTime class . Once we have found valid events, the next step is to modify the appearance of the cell containing the Calendar day. We will add a small icon (dot.gif) for each event by creating an Image control and adding the control to the Cell object's Controls collection. You can only add static controls, like an Image or HyperLink to the cell, nut not dynamic event raising controls like a DropDownList or Button. We also change the background color of the cell to draw more attention to the date. When a user selects a date in the Calendar by clicking on a day, the Calendar contol will fire the SelectionChanged event. We can catch this event in our code and display more details for the date selected. private void Calendar 1_SELECTIONCHANGED (Object Sender, System.EventArgs E) {

System.Data.DataView View = SocialeVents.defaultView;

View.rowfilter = string.format

"DATE> = # {0} # and date <# {1} #",

Calendar1.selectedDate.toshortdateString (),

Calendar1.selecteddddate.adddays (1) .toshortdateString ()

);

IF (view.count> 0)

{

DataGrid1.visible = true;

DataGrid1.datasource = view;

DataGrid1.databind ();

}

Else

{

DataGrid1.visible = false;

}

}

In the code above, we again build an expression, but this time for the DataView Filter property. By setting the proper filter expression we can see only those rows in the DataTable matching the selected date. Simple binding this new view of the DataTable to the DataGrid will give us all the details of our events. in the ASPX page we set the DataGrid Visible property to false, and override this property in code only if there are events to display. The columns of the grid are defined as follows.

The files Socials.aspx and Socials.aspx.cs are available to download. To experiment with these files, create a new web project in Visual Studio. Right click on the project in the Solution Explorer window and select "Add Existing Item". Browse To The Socials.aspx File to Import The Form Into The Project. This Code Should Give You The Basics To try Your OWN Calendar Control Experiments.

转载请注明原文地址:https://www.9cbs.com/read-110128.html

New Post(0)