日期控件Calendar的简单使用

       今天下午仿照着天轰穿大哥的视频学习日期控件Calendar,刚刚照着代码写了一遍,还是有很多都没有看懂啊。也不知道是一下子理解不了,还是我把代码打错了,达不到视频上讲的效果。还是把代码帖出来了。

None.gif public  partial  class  _Default : System.Web.UI.Page 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
string[][] arr;//声明数组来储存节日
InBlock.gif
    protected void Page_Init(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//填充日期列表
InBlock.gif
        for (int y = 1980; y < 2080; y++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Year.Items.Add(y.ToString());
ExpandedSubBlockEnd.gif        }

InBlock.gif        
for (int y = 1; y <= 12; y++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Mouth.Items.Add(y.ToString());
ExpandedSubBlockEnd.gif        }

InBlock.gif        
for (int y = 1; y <=31; y++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Day.Items.Add(y.ToString());
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        arr 
= new string[13][];
InBlock.gif        
for (int i = 0; i < 13; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            arr[i] 
= new string[32];
ExpandedSubBlockEnd.gif        }

InBlock.gif        
//下面是储存在数组中的节日
InBlock.gif
        arr[1][1= "元旦节";
InBlock.gif        arr[
2][14= "情人节";
InBlock.gif        arr[
3][8= "妇女节";
InBlock.gif        arr[
4][1= "愚人节";
InBlock.gif        arr[
5][1= "劳动节";
InBlock.gif        arr[
5][22= "赤色火焰的生日";
InBlock.gif        arr[
6][1= "儿童节";
InBlock.gif        arr[
7][1= "建党节";
InBlock.gif        arr[
8][1= "建军节";
InBlock.gif        arr[
9][10= "教师节";
InBlock.gif        arr[
10][1= "国庆节";
InBlock.gif        arr[
12][24= "平安夜";
InBlock.gif        arr[
12][25= "圣诞节";
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif    
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
switch (DropDownList1.SelectedValue)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            
case "None"://注意是冒号
InBlock.gif
                Calendar1.SelectionMode = CalendarSelectionMode.None;
InBlock.gif                
break;
InBlock.gif            
case "Day":
InBlock.gif                Calendar1.SelectionMode 
= CalendarSelectionMode.Day;
InBlock.gif                
break;
InBlock.gif            
case "DayWeek":
InBlock.gif                Calendar1.SelectionMode 
= CalendarSelectionMode.DayWeek;
InBlock.gif                
break;
InBlock.gif            
case "DayWeekMonth":
InBlock.gif                Calendar1.SelectionMode 
= CalendarSelectionMode.DayWeekMonth;
InBlock.gif                
break;
InBlock.gif        
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//当选择日期时激发
InBlock.gif

InBlock.gif        Label1.Text 
= Calendar1.SelectedDate.ToShortDateString();
InBlock.gif        Label2.Text 
= Calendar1.SelectedDate.Day.ToString()+"";
InBlock.gif        Label3.Text 
= Calendar1.SelectedDate.Month.ToString()+"";
InBlock.gif        Label4.Text 
= Calendar1.SelectedDate.Year.ToString() + "";
ExpandedSubBlockEnd.gif    }

InBlock.gif    
protected void Button1_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
string y = Year.SelectedValue.ToString();
InBlock.gif        
string m = Mouth.SelectedValue.ToString();
InBlock.gif        
string d = Day.SelectedValue.ToString();
InBlock.gif        Calendar1.VisibleDate 
= Convert.ToDateTime(y+"-"+m+"-"+d);//设置指定日期,要将日期转化为日期格式
ExpandedSubBlockEnd.gif
    }

InBlock.gif    
protected static DataTable ds(string day)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif//为日期将要绑定的数据库做个查询
InBlock.gif
        SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=sa;database=pubs;");
InBlock.gif        SqlDataAdapter sda 
= new SqlDataAdapter();
InBlock.gif        sda.SelectCommand 
= new SqlCommand("select title_id,pubdate,title from titles where pubdate="+day,con);
InBlock.gif        DataSet ds 
= new DataSet();
InBlock.gif        sda.Fill(ds,
"titles");
InBlock.gif        
return ds.Tables["titles"];
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
InBlock.gif    
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        CalendarDay d 
= e.Day;//设置当前日期
InBlock.gif
        TableCell c = e.Cell;//设置当前表格
InBlock.gif
        if(d.IsOtherMonth)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{//如果是其他月份的话就清除本来的节日
InBlock.gif
            c.Controls.Clear();
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string txt=arr[d.Date.Month][d.Date.Day];//得到完整的当前【月】【日】
InBlock.gif
                if(txt !=string.Empty)//看是否存在东西
ExpandedSubBlockStart.gifContractedSubBlock.gif
                dot.gif{
InBlock.gif                    
//仿照博客园的日历来做
InBlock.gif
                    string day=e.Day.Date.ToShortDateString();//获取当前日期并转化为短日期格式
InBlock.gif
                    foreach(DataRow dr in ds(day).Rows)//循环数据视图中的每一行
ExpandedSubBlockStart.gifContractedSubBlock.gif
                    dot.gif{
InBlock.gif                        c.Controls.Clear();
//清除所有表格中的元素
InBlock.gif
                        c.Controls.Add(new LiteralControl("<a href='http://mgod.cnblogs.com/haha.aspx?dt="+day+"'><b>"+e.Day.Date.Day+"</b></a>"));
ExpandedSubBlockEnd.gif                    }

InBlock.gif
InBlock.gif                    c.Controls.Add(
new LiteralControl("<br>"+txt));//如果有的话,就在日期数字后面换行显示出来了
ExpandedSubBlockEnd.gif
                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Response.Write(ex.ToString());
ExpandedSubBlockEnd.gif            }

InBlock.gif        
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}


点此下载代码

转载于:https://www.cnblogs.com/mgod/archive/2007/03/30/694473.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值