好久没提问题了,都生疏了,一个表头的问题,在线等!!!
时间:2011-12-07
来源:互联网
表头的代码如下:C# code
public DateTime now; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { now = System.DateTime.Now; } } protected void DataList2_ItemDataBound(object sender, DataListItemEventArgs e) { #region 写入表头日期 if (e.Item.ItemType == ListItemType.Header) //获取HeaderTemplate中的Label { //当日 string mouth = now.Month.ToString(); string day = now.Day.ToString(); string week = now.DayOfWeek.ToString(); Label label2 = e.Item.FindControl("Label2") as Label; label2.Text = mouth + "/" + day + " " + week; //第二天 DateTime now2 = System.DateTime.Now.AddDays(1); string mouth2 = now2.Month.ToString(); string day2 = now2.Day.ToString(); string week2 = now2.DayOfWeek.ToString(); Label label3 = e.Item.FindControl("Label3") as Label; label3.Text = mouth2 + "/" + day2 + " " + week2; //第三天 DateTime now3 = System.DateTime.Now.AddDays(2); string mouth3 = now3.Month.ToString(); string day3 = now3.Day.ToString(); string week3 = now3.DayOfWeek.ToString(); Label label4 = e.Item.FindControl("Label4") as Label; label4.Text = mouth3 + "/" + day3 + " " + week3; //第四天 DateTime now4 = System.DateTime.Now.AddDays(3); string mouth4 = now4.Month.ToString(); string day4 = now4.Day.ToString(); string week4 = now4.DayOfWeek.ToString(); Label label5 = e.Item.FindControl("Label5") as Label; label5.Text = mouth4 + "/" + day4 + " " + week4; //第五天 DateTime now5 = System.DateTime.Now.AddDays(4); string mouth5 = now5.Month.ToString(); string day5 = now5.Day.ToString(); string week5 = now5.DayOfWeek.ToString(); Label label6 = e.Item.FindControl("Label6") as Label; label6.Text = mouth5 + "/" + day5 + " " + week5; //第六天 DateTime now6 = System.DateTime.Now.AddDays(5); string mouth6 = now6.Month.ToString(); string day6 = now6.Day.ToString(); string week6 = now6.DayOfWeek.ToString(); Label label7 = e.Item.FindControl("Label7") as Label; label7.Text = mouth6 + "/" + day6 + " " + week6; //第七天 DateTime now7 = System.DateTime.Now.AddDays(6); string mouth7 = now7.Month.ToString(); string day7 = now7.Day.ToString(); string week7 = now7.DayOfWeek.ToString(); Label label8 = e.Item.FindControl("Label8") as Label; label8.Text = mouth7 + "/" + day7 + " " + week7; } #endregion }
然后我有个按钮是上一周,我这样写,报错,请问应该怎么写?
C# code
protected void Button1_Click(object sender, EventArgs e)//上一周 { now = now.AddDays(-1); }
作者: generhappy 发布时间: 2011-12-07
now 为null了 未将对象引用到实例了
作者: liukaizxc 发布时间: 2011-12-07
C# code
protected void Page_Load(object sender, EventArgs e) { now = System.DateTime.Now; }
作者: Return_false 发布时间: 2011-12-07
作者: q107770540 发布时间: 2011-12-07
{
now = System.DateTime.Now;
}
now = System.DateTime.Now;
不要放在IsPostBack 里
作者: Sandy945 发布时间: 2011-12-07
改成
C# code
protected void Page_Load(object sender, EventArgs e)
{
now = System.DateTime.Now;
}
改成这样之后,确实不报错了,断点后台也正确了,可是这样,这个“上一周”的按钮只能点击一次了,再点上一周就不管用了,怎么才能记录状态呢?
作者: generhappy 发布时间: 2011-12-07
作者: Sandy945 发布时间: 2011-12-07
ViewState
不太熟悉viewstate,请给点代码提示下,好不好?
作者: generhappy 发布时间: 2011-12-07
label2.Text = ...
这种写法。。。。
作者: guanlianwei 发布时间: 2011-12-07
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28