+ -
当前位置:首页 → 问答吧 → 好久没提问题了,都生疏了,一个表头的问题,在线等!!!

好久没提问题了,都生疏了,一个表头的问题,在线等!!!

时间:2011-12-07

来源:互联网

我用的控件是datalist,表头是可以变化的

表头的代码如下: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 = now.AddDays(-1);
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

if (!IsPostBack)
  {
now = System.DateTime.Now;
}

now = System.DateTime.Now;
不要放在IsPostBack 里

作者: Sandy945   发布时间: 2011-12-07

引用 2 楼 return_false 的回复:
改成

C# code
protected void Page_Load(object sender, EventArgs e)
{
now = System.DateTime.Now;
}


改成这样之后,确实不报错了,断点后台也正确了,可是这样,这个“上一周”的按钮只能点击一次了,再点上一周就不管用了,怎么才能记录状态呢?

作者: generhappy   发布时间: 2011-12-07

ViewState

作者: Sandy945   发布时间: 2011-12-07

引用 6 楼 sandy945 的回复:
ViewState


不太熟悉viewstate,请给点代码提示下,好不好?

作者: generhappy   发布时间: 2011-12-07

Label label2 = ...
label2.Text = ...

这种写法。。。。

作者: guanlianwei   发布时间: 2011-12-07