TinyMind
.net.cn
首页
资讯
文章
注册
登录
VS2.0控件之日历《Calendar》C#
大牛博客
关注文章
TinyMind专栏频道
下面是今天用C#做的例题
前台HTML代码
<%
@PageLanguage
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
Default.aspx.cs
"
Inherits
=
"
_Default
"
%>
<!
DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
<
html
xmlns
="http://www.w3.org/1999/xhtml"
>
<
head
runat
="server"
>
<
title
>
无标题页
</
title
>
</
head
>
<
body
>
<
form
id
="form1"
runat
="server"
>
<
div
>
控制日历的选择模式:
<
br
/>
<
asp:DropDownList
ID
="DropDownList1"
runat
="server"
AutoPostBack
="True"
OnSelectedIndexChanged
="DropDownList1_SelectedIndexChanged"
>
<
asp:ListItem
Value
="None"
>
无选择
</
asp:ListItem
>
<
asp:ListItem
Value
="Day"
>
天
</
asp:ListItem
>
<
asp:ListItem
Value
="DayWeek"
>
天、周
</
asp:ListItem
>
<
asp:ListItem
Value
="DayWeekMonth"
>
天、周、月
</
asp:ListItem
>
</
asp:DropDownList
>
<
asp:Calendar
ID
="Calendar1"
runat
="server"
BackColor
="#FFFFCC"
BorderColor
="#FFCC66"
BorderWidth
="1px"
DayNameFormat
="Shortest"
Font-Names
="Verdana"
Font-Size
="8pt"
ForeColor
="#663399"
Height
="200px"
NextMonthText
="下月"
NextPrevFormat
="ShortMonth"
OnSelectionChanged
="Calendar1_SelectionChanged"
PrevMonthText
="上月"
SelectMonthText
="选全月"
SelectWeekText
="选全周"
ShowGridLines
="True"
Width
="328px"
DayHeaderStyle-Font-Italic
="true"
OnDayRender
="Calendar1_DayRender"
OnVisibleMonthChanged
="Calendar1_VisibleMonthChanged"
>
<
SelectedDayStyle
BackColor
="#CCCCFF"
BorderStyle
="Double"
Font-Bold
="True"
/>
<
TodayDayStyle
BackColor
="#FFCC66"
ForeColor
="White"
/>
<
SelectorStyle
BackColor
="#FFCC66"
/>
<
OtherMonthDayStyle
ForeColor
="#CC9966"
/>
<
NextPrevStyle
Font-Size
="9pt"
ForeColor
="#FFFFCC"
/>
<
DayHeaderStyle
BackColor
="#FFCC66"
Font-Bold
="True"
Height
="1px"
Font-Italic
="True"
/>
<
TitleStyle
BackColor
="#990000"
Font-Bold
="True"
Font-Size
="9pt"
ForeColor
="#FFFFCC"
/>
<
WeekendDayStyle
BackColor
="LightSalmon"
/>
</
asp:Calendar
>
<
br
/>
<
br
/>
<
asp:Label
ID
="Label1"
runat
="server"
></
asp:Label
><
br
/>
选择的月是:
<
asp:Label
ID
="Label2"
runat
="server"
></
asp:Label
><
br
/>
选择的年是:
<
asp:Label
ID
="Label4"
runat
="server"
></
asp:Label
><
br
/>
<
br
/>
显示输入的时间
<
asp:DropDownList
ID
="ye"
runat
="server"
>
</
asp:DropDownList
>
年
<
asp:DropDownList
ID
="mo"
runat
="server"
>
</
asp:DropDownList
>
月
<
asp:DropDownList
ID
="da"
runat
="server"
>
</
asp:DropDownList
>
日
<
asp:Button
ID
="Button1"
runat
="server"
OnClick
="Button1_Click"
Text
="切"
Width
="85px"
/><
br
/>
<
br
/>
</
div
>
</
form
>
</
body
>
</
html
>
后台C#代码
using
System;
using
System.Data;
using
System.Configuration;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
public
partial
class
_Default:System.Web.UI.Page
{
string
[][]arr;
//
声明一个数组来存储一年中的节日
protected
void
Page_Load(
object
sender,EventArgse)
{
arr
=
new
string
[
13
][];
//
数组
for
(
int
i
=
0
;i
<
13
;i
++
)
{
arr[i]
=
new
string
[
32
];
}
//
下面是储存在数组中的节日
arr[
1
][
1
]
=
"
圆蛋节
"
;
arr[
2
][
15
]
=
"
没啥节样
"
;
arr[
3
][
8
]
=
"
妇女节
"
;
arr[
4
][
15
]
=
"
没啥节样
"
;
arr[
5
][
1
]
=
"
大假哦
"
;
arr[
6
][
1
]
=
"
娃娃节
"
;
arr[
7
][
1
]
=
"
jiandang节
"
;
arr[
7
][
15
]
=
"
洪川医药正式启用
"
;
arr[
8
][
1
]
=
"
兵仔儿节
"
;
arr[
9
][
10
]
=
"
孔子节
"
;
arr[
9
][
28
]
=
"
热烈庆祝,今天是川哥的生日哈!
"
;
arr[
10
][
1
]
=
"
大假哦
"
;
arr[
11
][
15
]
=
"
没啥节样
"
;
arr[
12
][
25
]
=
"
鬼节
"
;
//
填充年月日下拉菜单
for
(
int
y
=
1980
;y
<
2050
;y
++
)
{
//
填充年下拉列表
ye.Items.Add(y.ToString());
}
for
(
int
m
=
1
;m
<
13
;m
++
)
{
//
填充月下拉列表
mo.Items.Add(m.ToString());
}
for
(
int
d
=
1
;d
<
32
;d
++
)
{
//
填充日下拉列表
da.Items.Add(d.ToString());
}
}
protected
void
DropDownList1_SelectedIndexChanged(
object
sender,EventArgse)
{
switch
(DropDownList1.SelectedValue)
//
循环判断
{
case
"
None
"
:
//
注意是冒号
Calendar1.SelectionMode
=
CalendarSelectionMode.None;
//
设置选择方式为没有
break
;
//
跳出循环
case
"
DayWeekMonth
"
:
Calendar1.SelectionMode
=
CalendarSelectionMode.DayWeekMonth;
break
;
case
"
DayWeek
"
:
Calendar1.SelectionMode
=
CalendarSelectionMode.DayWeek;
break
;
case
"
Day
"
:
Calendar1.SelectionMode
=
CalendarSelectionMode.Day;
break
;
}
}
protected
void
Calendar1_SelectionChanged(
object
sender,EventArgse)
{
//
当选择一个日期时激发
Label1.Text
=
"
完整日期是:
"
+
Calendar1.SelectedDate.ToShortDateString();
Label2.Text
=
Calendar1.SelectedDate.Month.ToString()
+
"
月
"
;
Label4.Text
=
Calendar1.SelectedDate.Year.ToString()
+
"
年
"
;
}
protected
void
Calendar1_VisibleMonthChanged(
object
sender,MonthChangedEventArgse)
{
//
当切换到其他月的时候激发
Label1.Text
=
"
你真的要换到下个月??
"
;
}
protected
void
Button1_Click(
object
sender,EventArgse)
{
//
指定日期事件
string
y
=
ye.SelectedValue.ToString();
//
得到年的值
string
m
=
mo.SelectedValue.ToString();
//
得到月的值
string
d
=
da.SelectedValue.ToString();
//
得到日的值
Calendar1.VisibleDate
=
Convert.ToDateTime(y
+
"
-
"
+
m
+
"
-
"
+
d);
//
指定被选中的日期
}
protected
void
Calendar1_DayRender(
object
sender,DayRenderEventArgse)
//
注意这个事件名称是DayRender
{
CalendarDayd
=
e.Day;
//
设置当前日期
TableCellc
=
e.Cell;
//
设置当前表格
if
(d.IsOtherMonth)
{
//
如果是其他月份的话就清除本来的节日
c.Controls.Clear();
}
else
{
try
{
string
txt
=
arr[d.Date.Month][d.Date.Day];
//
得到完整的当前[月][日]
if
(txt
!=
string
.Empty)
//
看下里面是否有东西
{
c.Controls.Add(
new
LiteralControl(
"
<br>
"
+
txt));
//
如果有的话,就在日期数字后面换行显示出来
}
}
catch
(Exceptionexe)
{
Response.Write(exe.ToString());
}
}
}
}
优质内容筛选与推荐
>>
1、别被数据分析师骗了!用数据说谎的三种办法
2、为什么程序员总是发现不了自己的Bug?程序员:我不认识他啊
3、android插件开发机制
4、2018谷歌开发者大会原声中文字幕版视频
5、码云推荐|基于Hadoop的KafkaOnYarn
赞赏
长按二维码向我转账
受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。
阅读
好看
已推荐到看一看
你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
取消
推荐
我知道了
已取消,“好看”想法已同步删除
知道了
已推荐到看一看
和朋友分享想法
最多200字,当前共
字
发送
已发送
朋友将在看一看看到
确定
分享你的想法...
取消
分享想法到看一看
确定
最多200字,当前共
字
发送中
网络异常,请稍后重试
知道了
微信扫一扫
关注该公众号
0
│
收藏
│
举报