您现在的位置:首页 >> Asp.NET >> 内容

C# calendar 日历代码全解析

时间:2013-9-18 14:03:08 点击:

  核心提示:代码如下:using System;………………public partial class Default: System.Web.UI.Page{ protected void Page_Load(o...

代码如下:

using System;
………………

public partial class Default: System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack )                                      //如果第一次加载页面使日历,label不可见
        {
            Calendar1.Visible = false;
            Label1.Visible = false;
            for (int y=1949;y<2050 ;y++ )             //给日期1下拉框添加年
            {
                DropDownList1.Items.Add(y.ToString());
            }
            for (int m = 1; m < 13;m++ )               //给日期2下拉框添加月
            {
                DropDownList2.Items.Add(m.ToString());
            }
            DropDownList1.Text = (DateTime.Now.Year).ToString();        //给下拉框默认显示当前日期
            DropDownList2.Text = (DateTime.Now.Month).ToString();
        }
    }

//当下拉框变化时让日历显示行对应的日期
    protected void DropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {
        string y = DropDownList1.SelectedValue;
        string m = DropDownList2.SelectedValue;
        Calendar1.VisibleDate = Convert.ToDateTime(y+"-"+m+"-"+(Calendar1.SelectedDate.Day).ToString());
        Calendar1.SelectedDate = Convert.ToDateTime(y + "-" + m + "-" + (Calendar1.SelectedDate.Day).ToString());
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Calendar1.Visible = true;         //当点击linkbutton的时候才弹出日历
    }
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        DropDownList2.Text = Calendar1.SelectedDate.Month.ToString(); //当日期通过day从一个月变化到另一个月时dropdownlist的月也跟着变化
    }
    protected void MonthChanged(object sender, MonthChangedEventArgs e)
    {
        DropDownList1.Text = e.NewDate.Year.ToString();//当点击日历上的上下月时dropdownlist的年和月也跟着变化
        DropDownList2.Text = e.NewDate.Month.ToString();
       
        
    }
    protected void Button1_Click(object sender, EventArgs e)//触发单击事件,让label显示,把选择的日期显示出来
    {
        Label1.Visible = true;
        Label1.Text = (Calendar1.SelectedDate.Year).ToString() +"-"+ (Calendar1.SelectedDate.Month).ToString() +"-"+ (Calendar1.SelectedDate.Day).ToString();
    }
}

设计页面代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//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 style="text-align: left">
        <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">选择你的日期</asp:LinkButton>&nbsp;
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList_SelectedIndexChanged">
        </asp:DropDownList>
        年
        <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList_SelectedIndexChanged">
        </asp:DropDownList>
        月
        <asp:Calendar ID="Calendar1" runat="server" BackColor="#FFFFCC" BorderColor="#FFCC66" Font-Names="Verdana" Font-Size="8pt" ForeColor="#663399"
            Height="200px" Width="220px" BorderWidth="1px" DayNameFormat="Shortest" ShowGridLines="True" OnVisibleMonthChanged="MonthChanged" OnSelectionChanged="Calendar1_SelectionChanged" Visible="False">
            <SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" />
            <TodayDayStyle BackColor="#FFCC66" ForeColor="White" />
            <OtherMonthDayStyle ForeColor="#CC9966" />
            <NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" />
            <DayHeaderStyle Font-Bold="True" Height="1px" BackColor="#FFCC66" />
            <TitleStyle BackColor="#990000" Font-Bold="True" Font-Size="9pt"
                ForeColor="#FFFFCC" />
            <SelectorStyle BackColor="#FFCC66" />
        </asp:Calendar>
        &nbsp;
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="确定" /><br />
        &nbsp;<asp:Label ID="Label1" runat="server" ForeColor="Fuchsia"></asp:Label>&nbsp;<br />
    
    </div>
    </form>
</body>
</html>

作者:不详 来源:网络
共有评论 0相关评论
发表我的评论
  • 大名:
  • 内容:
  • 青春网络编程技术(www.qcfupo.com) © 2020 版权所有 All Rights Reserved.
  • Powered by qcfupo! V3.0sp1
  • 百度友情链接: 百家乐[www.chinawestnews.net/aomenbaijiale/] 赌博网[www.chinawestnews.net/amdbwz/] 海立方[www.bjl.gov.cn/bjgylc/] 娱乐城[www.gtxrmzf.gov.cn/mapai/] 娱乐城[www.hfsf.gov.cn/yfgj/] 威尼斯人[www.hllinkou.gov.cn/baijile/] 澳门威尼斯[www.jjwgj.gov.cn/wnsr/] 888真人[www.lagm.gov.cn/888zhenren/] 百乐坊[www.laoling.gov.cn/blfylc/] 现金网[www.longyu.gov.cn/xjw/] 百家乐[www.chinawestnews.net/ambjl/] 新葡京娱乐场[www.sjzedu.gov.cn/jsylc/] 时时彩[www.snjw.gov.cn/ssc/] 赌博[www.chinawestnews.net/aomendubo/]