Asp. NET notes of the calender use of instructions _ practical skills

Source: Internet
Author: User

1. Introduction

(1, in calender, all selectable symbols appear underlined because they are rendered as links in the browser.)

If you want the user to be able to select a day, month, or week, you must set the SelectionMode property (days, DayWeek, DayWeekMonth)

(2 control events when the user chooses a day or month, you can use onselectionchanged to trigger

To get the selected point of Time by Calendar1.SelectedDate.ToShortDateString ();
Get the selected number of days by Calendar1.SelectedDate.Count.ToString ();

2. Example

Now an example is used to deepen the understanding of the Calendar control:

When you click Tgif, all Fridays for the selected month are displayed on the calendar

When you click Apply, the date of the start to end is displayed on the calendar

Calender.aspx.cs

Copy Code code as follows:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;

public partial class MyTest_Calender:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
if (! IsPostBack) {
My_calendar.visibledate = my_calendar.todaysdate;
Maps the months in a selected date to a Drop-down box
Month_list.selectedindex = my_calendar.visibledate.month-1;
}
Show to label
Lbltodaysdate.text = "Today is:" + my_Calendar.TodaysDate.ToShortDateString ();
}
Triggers a function when a date is selected
protected void Calendar_select (object sender, EventArgs e)
{
Lblcountupdate ();
Lblselectedupdate ();
Txtclear ();
}
Drop-down Box Trigger function
protected void Month_selectedchange (object sender, EventArgs e)
{
My_Calendar.SelectedDates.Clear ();
Lblselectedupdate ();
Lblcountupdate ();
Reset Time
My_calendar.visibledate = new DateTime (My_Calendar.VisibleDate.Year,
Int32.Parse (Month_List.SelectedItem.Value), 1);
Txtclear ();
}

refactoring function 01-Number of modified dates
private void Lblcountupdate () {
Lblcount.text = "The Count of Selected:" + my_Calendar.SelectedDates.Count.ToString ();
}
Refactoring function 02-Clear data
private void Txtclear () {
Txtend.text = "";
Txtstart.text = "";
}
Refactoring function 03-Modified Date
private void Lblselectedupdate () {
if (my_calendar.selecteddate!= datetime.minvalue)
Lblselcteddate.text = "The selected day is:" +
My_Calendar.SelectedDate.ToShortDateString ();
}
Button 1: Displays all Fridays for the selected month
protected void Btntgif_click (object sender, EventArgs e)
{
int currnetmonth = My_Calendar.VisibleDate.Month;
int curretnyear = My_Calendar.VisibleDate.Year;
First clear the original calendar location
My_Calendar.SelectedDates.Clear ();
Add it to the calendar if the date is Friday
for (int i = 1; I <= System.DateTime.DaysInMonth (
Curretnyear, Currnetmonth); i++) {

datetime datetime = new DateTime (Curretnyear, Currnetmonth, i);
if (datetime). DayOfWeek = = Dayofweek.friday)
MY_CALENDAR.SELECTEDDATES.ADD (datetime);
}
Lblselectedupdate ();
Lblcountupdate ();
}
Button 2:
protected void Btnrange_click (object sender, EventArgs e)
{
int currnetmonth = My_Calendar.VisibleDate.Month;
int curretnyear = My_Calendar.VisibleDate.Year;

DateTime startdate = new DateTime (Curretnyear, Currnetmonth,int32.parse (Txtstart.text));
DateTime endtartdate = new DateTime (Curretnyear, Currnetmonth, Int32.Parse (Txtend.text));
My_Calendar.SelectedDates.Clear ();
My_Calendar.SelectedDates.SelectRange (startdate,endtartdate);

Lblcountupdate ();
Lblselectedupdate ();
}
}

calender.aspx
Copy Code code as follows:

<%@ Page language= "C #" autoeventwireup= "true" codefile= "Calender.aspx.cs" inherits= "Mytest_calender"%>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<title>calender control</title>
<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:calendar id= "My_calendar" runat= "Server"
onselectionchanged= "Calendar_select" >
</asp:Calendar>
<br/>
<asp:label id= "Lblcount" runat= "Server" text= "Label" ></asp:Label>
<br/>
<asp:label id= "lbltodaysdate" runat= "Server" text= "Label" ></asp:Label>
<br/>
<asp:label id= "lblselcteddate" runat= "Server" text= "Label" ></asp:Label>

<table>
<tr>
<td>select a month</td>
<td>
<asp:dropdownlist id= "month_list" runat= "Server"
Autopostback= "true"
onselectedindexchanged= "Month_selectedchange" >
<asp:listitem text= "January" value= "1"/>
<asp:listitem text= "February" value= "2"/>
<asp:listitem text= "March" value= "3"/>
<asp:listitem text= "April" value= "4"/>
<asp:listitem text= "may" value= "5"/>
<asp:listitem text= "June" value= "6"/>
<asp:listitem text= "July" value= "7"/>
<asp:listitem text= "Augut" value= "8"/>
<asp:listitem text= "September" value= "9"/>
<asp:listitem text= "October" value= "ten"/>
<asp:listitem text= "November" value= "one"/>
<asp:listitem text= "December" value= "/>"
</asp:DropDownList>
</td>
<td>
<asp:button id= "btntgif" runat= "Server"
text= "TGIF"
onclick= "Btntgif_click"/>
</td>
</tr>
<tr>
&LT;TD colspan= "2" > </td>
</tr>
<tr>
&LT;TD colspan= "2" ><b>day range</b></td>
</tr>

<tr>
&LT;TD >starting day</td>
&LT;TD >ending day</td>
</tr>

<tr>
&LT;TD >
<asp:textbox id= "Txtstart" runat= "Server"
Width= "Maxlength=" "2" ></asp:TextBox>
</td>
&LT;TD >
<asp:textbox id= "Txtend" runat= "Server"
Width= "Maxlength=" "2" ></asp:TextBox>
</td>
&LT;TD >
<asp:button id= "Btnrange" runat= "Server" text= "Apply"
onclick= "Btnrange_click" style= "height:21px"/>
</td>
</tr>
</table>
</div>
</form>
</body>

Summary:

(1 with some refactoring, some function methods are separated, this piece has not been separated completely

(The 2nd control also has a visiblmonthchanged event to handle whether the calendar was changed by the user for the month, E.newdate.year and E.) Previousdate.year the sort of comparison

(3 DayRender events, you can use the cell and day to render the specific date, such as the color of weekends and holidays,

E.day.isothermonth E.day.isweekend, etc.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.