Subscribe

RSS Feed (xml)

Displaying a Calendar in a Web Form in ASP.NET

You can use the CalendarWeb Control to display a calendar on a page or as a selection
tool on a form. Its sub-elements appearance can all be set using its properties.To display
a calendar with notes on some days:
<script runat=”server”>
Sub PrettyCalendar_OnDayRender (Sender As Object, _
e As System.Web.UI.WebControls.DayRenderEventArgs)
If e.Day.Date = Today Then
‘Add a carriage return
e.Cell.Controls.Add(new LiteralControl(“<br />”))
‘Add a message of the day
e.Cell.Controls.Add(new LiteralControl(“Today!”))
End If
End Sub
</script>
<form runat=”server”>
<asp:Calendar ID=”PrettyCalendar” Runat=”server”
OnDayRender=”PrettyCalendar_OnDayRender”>
<TodayDayStyle ForeColor=”Red” BackColor=”Aqua”></TodayDayStyle>
<DayStyle Font-Bold=”True” HorizontalAlign=”Left” Height=”90px”
BorderWidth=”1px” BorderStyle=”Solid” BorderColor=”Blue” Width=”100px”
VerticalAlign=”Top” BackColor=”#80FFFF”></DayStyle>
<NextPrevStyle ForeColor=”Blue”></NextPrevStyle>
<DayHeaderStyle Font-Size=”Large” Font-Bold=”True” BorderWidth=”1px”
ForeColor=”Brown” BorderStyle=”Solid” BorderColor=”Black” Width=”100px”
BackColor=”#00C0C0”></DayHeaderStyle>
<TitleStyle Font-Size=”Large” Font-Bold=”True” BorderWidth=”1px”
BorderStyle=”Solid” BorderColor=”Black” BackColor=”#80FFFF”></TitleStyle>
<WeekendDayStyle BackColor=”#C0C0FF”></WeekendDayStyle>
</asp:Calendar>
</form>
To use a calendar for user input on a Web Form:
<%@ Page Language=”VB” Explicit=”true” Strict=”true” %>
<HTML>
<body>
<script runat=”server”>
Sub Page_Load(Sender As object, e As EventArgs)
If Not IsPostBack Then
DepartureDate.SelectedDate = System.DateTime.Today
End If
End Sub
Sub SearchButton_Click(Sender As object, e As EventArgs)
If DepartureDate.SelectedDate <= System.DateTime.Today Then
ResultLabel.Text = _
“You must selected a Departure Date in the future.”
ElseIf(ReturnDate.SelectedDate < DepartureDate.SelectedDate)
ResultLabel.Text = “Return Date must follow the Departure Date.”
Else
ResultLabel.Text = “Departing on “ & _
DepartureDate.SelectedDate.ToShortDateString() & _
“ and returning on “ & _
ReturnDate.SelectedDate.ToShortDateString()
End If
End Sub
</script>
<form runat=”server” ID=”Form1”>
<h1>Find a Flight</h1>
<table>
<tr>
<td>Departure Date</td>
<td><asp:Calendar ID=”DepartureDate” Runat=”server”>
<DayStyle Font-Size=”X-Small”></DayStyle>
<TitleStyle Font-Bold=”True”></TitleStyle>
</asp:Calendar></td>
</tr>
<tr>
<td>Return Date</td>
<td><asp:Calendar ID=”ReturnDate” Runat=”server”>
<DayStyle Font-Size=”X-Small”></DayStyle>
<TitleStyle Font-Bold=”True”></TitleStyle>
</asp:Calendar></td>
</tr>
</table>
<asp:Button ID=”SearchButton” Runat=”server”
Text=”Search” OnClick=”SearchButton_Click” />
<br>
<asp:Label ID=”ResultLabel” Runat=”server” />
</form>
</body>
</HTML>

You can extend the Calendar control a number of ways, such as allowing users to select
certain weeks or months or ranges of dates. It can be used to display events on certain
dates, and its appearance can be controlled by its properties or through the use of
Cascading Style Sheets. It does not support the use of the standard validation controls,
but a CustomValidator can be used to validate a calendar, if necessary.

No comments:

Variety in the Web World