今天下午仿照着天轰穿大哥的视频学习日期控件Calendar,刚刚照着代码写了一遍,还是有很多都没有看懂啊。也不知道是一下子理解不了,还是我把代码打错了,达不到视频上讲的效果。还是把代码帖出来了。
点此下载代码
public partial class _Default : System.Web.UI.Page
{
string[][] arr;//声明数组来储存节日
protected void Page_Init(object sender, EventArgs e)
{
//填充日期列表
for (int y = 1980; y < 2080; y++)
{
Year.Items.Add(y.ToString());
}
for (int y = 1; y <= 12; y++)
{
Mouth.Items.Add(y.ToString());
}
for (int y = 1; y <=31; y++)
{
Day.Items.Add(y.ToString());
}
arr = new string[13][];
for (int i = 0; i < 13; i++)
{
arr[i] = new string[32];
}
//下面是储存在数组中的节日
arr[1][1] = "元旦节";
arr[2][14] = "情人节";
arr[3][8] = "妇女节";
arr[4][1] = "愚人节";
arr[5][1] = "劳动节";
arr[5][22] = "赤色火焰的生日";
arr[6][1] = "儿童节";
arr[7][1] = "建党节";
arr[8][1] = "建军节";
arr[9][10] = "教师节";
arr[10][1] = "国庆节";
arr[12][24] = "平安夜";
arr[12][25] = "圣诞节";
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
switch (DropDownList1.SelectedValue)
{
case "None"://注意是冒号
Calendar1.SelectionMode = CalendarSelectionMode.None;
break;
case "Day":
Calendar1.SelectionMode = CalendarSelectionMode.Day;
break;
case "DayWeek":
Calendar1.SelectionMode = CalendarSelectionMode.DayWeek;
break;
case "DayWeekMonth":
Calendar1.SelectionMode = CalendarSelectionMode.DayWeekMonth;
break;
}
}
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
//当选择日期时激发
Label1.Text = Calendar1.SelectedDate.ToShortDateString();
Label2.Text = Calendar1.SelectedDate.Day.ToString()+"日";
Label3.Text = Calendar1.SelectedDate.Month.ToString()+"月";
Label4.Text = Calendar1.SelectedDate.Year.ToString() + "年";
}
protected void Button1_Click(object sender, EventArgs e)
{
string y = Year.SelectedValue.ToString();
string m = Mouth.SelectedValue.ToString();
string d = Day.SelectedValue.ToString();
Calendar1.VisibleDate = Convert.ToDateTime(y+"-"+m+"-"+d);//设置指定日期,要将日期转化为日期格式
}
protected static DataTable ds(string day)
{ //为日期将要绑定的数据库做个查询
SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=sa;database=pubs;");
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = new SqlCommand("select title_id,pubdate,title from titles where pubdate="+day,con);
DataSet ds = new DataSet();
sda.Fill(ds,"titles");
return ds.Tables["titles"];
}
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
CalendarDay d = e.Day;//设置当前日期
TableCell c = e.Cell;//设置当前表格
if(d.IsOtherMonth)
{//如果是其他月份的话就清除本来的节日
c.Controls.Clear();
}
else
{
try
{
string txt=arr[d.Date.Month][d.Date.Day];//得到完整的当前【月】【日】
if(txt !=string.Empty)//看是否存在东西
{
//仿照博客园的日历来做
string day=e.Day.Date.ToShortDateString();//获取当前日期并转化为短日期格式
foreach(DataRow dr in ds(day).Rows)//循环数据视图中的每一行
{
c.Controls.Clear();//清除所有表格中的元素
c.Controls.Add(new LiteralControl("<a href='http://mgod.cnblogs.com/haha.aspx?dt="+day+"'><b>"+e.Day.Date.Day+"</b></a>"));
}
c.Controls.Add(new LiteralControl("<br>"+txt));//如果有的话,就在日期数字后面换行显示出来了
}
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
}
}
{
string[][] arr;//声明数组来储存节日
protected void Page_Init(object sender, EventArgs e)
{
//填充日期列表
for (int y = 1980; y < 2080; y++)
{
Year.Items.Add(y.ToString());
}
for (int y = 1; y <= 12; y++)
{
Mouth.Items.Add(y.ToString());
}
for (int y = 1; y <=31; y++)
{
Day.Items.Add(y.ToString());
}
arr = new string[13][];
for (int i = 0; i < 13; i++)
{
arr[i] = new string[32];
}
//下面是储存在数组中的节日
arr[1][1] = "元旦节";
arr[2][14] = "情人节";
arr[3][8] = "妇女节";
arr[4][1] = "愚人节";
arr[5][1] = "劳动节";
arr[5][22] = "赤色火焰的生日";
arr[6][1] = "儿童节";
arr[7][1] = "建党节";
arr[8][1] = "建军节";
arr[9][10] = "教师节";
arr[10][1] = "国庆节";
arr[12][24] = "平安夜";
arr[12][25] = "圣诞节";
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
switch (DropDownList1.SelectedValue)
{
case "None"://注意是冒号
Calendar1.SelectionMode = CalendarSelectionMode.None;
break;
case "Day":
Calendar1.SelectionMode = CalendarSelectionMode.Day;
break;
case "DayWeek":
Calendar1.SelectionMode = CalendarSelectionMode.DayWeek;
break;
case "DayWeekMonth":
Calendar1.SelectionMode = CalendarSelectionMode.DayWeekMonth;
break;
}
}
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
//当选择日期时激发
Label1.Text = Calendar1.SelectedDate.ToShortDateString();
Label2.Text = Calendar1.SelectedDate.Day.ToString()+"日";
Label3.Text = Calendar1.SelectedDate.Month.ToString()+"月";
Label4.Text = Calendar1.SelectedDate.Year.ToString() + "年";
}
protected void Button1_Click(object sender, EventArgs e)
{
string y = Year.SelectedValue.ToString();
string m = Mouth.SelectedValue.ToString();
string d = Day.SelectedValue.ToString();
Calendar1.VisibleDate = Convert.ToDateTime(y+"-"+m+"-"+d);//设置指定日期,要将日期转化为日期格式
}
protected static DataTable ds(string day)
{ //为日期将要绑定的数据库做个查询
SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=sa;database=pubs;");
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = new SqlCommand("select title_id,pubdate,title from titles where pubdate="+day,con);
DataSet ds = new DataSet();
sda.Fill(ds,"titles");
return ds.Tables["titles"];
}
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
CalendarDay d = e.Day;//设置当前日期
TableCell c = e.Cell;//设置当前表格
if(d.IsOtherMonth)
{//如果是其他月份的话就清除本来的节日
c.Controls.Clear();
}
else
{
try
{
string txt=arr[d.Date.Month][d.Date.Day];//得到完整的当前【月】【日】
if(txt !=string.Empty)//看是否存在东西
{
//仿照博客园的日历来做
string day=e.Day.Date.ToShortDateString();//获取当前日期并转化为短日期格式
foreach(DataRow dr in ds(day).Rows)//循环数据视图中的每一行
{
c.Controls.Clear();//清除所有表格中的元素
c.Controls.Add(new LiteralControl("<a href='http://mgod.cnblogs.com/haha.aspx?dt="+day+"'><b>"+e.Day.Date.Day+"</b></a>"));
}
c.Controls.Add(new LiteralControl("<br>"+txt));//如果有的话,就在日期数字后面换行显示出来了
}
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
}
}
点此下载代码