- 爱易网页
 
                        - 
                            ASP.NET教程
 
                        - 为什么按钮对应的点击事件只执行了一次,再点击就无效了 
 
                         
                    
                    
                    日期:2014-05-17  浏览次数:20492 次 
                    
                        
                         为何按钮对应的点击事件只执行了一次,再点击就无效了?
public partial class _Default : System.Web.UI.Page
{
     public void Show_Works()
     {
         if (Request["username"] == null)
         {
             Response.Write("<script LANGUAGE='javascript'>alert('您的用户名有误!');window.close();</script>");
         }
         else
         {
             string username = Request["username"].ToString().Trim();
             Session["username"] = username;
             SqlConnection con = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["carConnectionString"].ToString());
             {
                 //判断power看是否有权限使用,或者username是否正确
                 string cmd1 = "select * from powers where username='" + username + "' and power=1";
                 SqlDataAdapter da = new SqlDataAdapter(cmd1, con);
                 DataSet ds = new DataSet();//创建数据集
                 da.Fill(ds);//填充数据集
                 DataTable dt = ds.Tables[0];
                 if (dt.Rows.Count == 0)
                 {
                     Response.Write("<script LANGUAGE='javascript'>alert('您没有权限使用该功能!');window.close();</script>");
                 }
                 //查找和显示今天的工作
                 cmd1 = "select * from works where username='" + username + "' and wdate='" + Session["times"].ToString() + "'";
                 da = new SqlDataAdapter(cmd1, con);
                 ds = new DataSet();//创建数据集
                 da.Fill(ds);//填充数据集
                 dt = ds.Tables[0];
                 for (int i = 0; i < dt.Rows.Count; i++)
                 {
                     Label lb = new Label();
                     lb.Text = dt.Rows[i]["wcontain"].ToString().Trim();
                     lb.CssClass = "lbs";
                     lb.Attributes.Add("ondblclick", "justtest()");
                     lb.ID = dt.Rows[i]["wid"].ToString().Trim();
                     this.Panel1.Controls.Add(lb);
                 }
             }
         }
     }
     protected void Page_Load(object sender, EventArgs e)
     {
         Session["times"] = DateTime.Now.Date.ToString();
         Show_Works();
     }
     protected void Next_Click(object sender, EventArgs e)
     {
         this.Panel1.Controls.Clear();
         Session["times"] = DateTime.Parse(Session["times"].ToString()).AddDays(1).ToString();
         Show_Works();
     }
}
============================================================================================
只要看Next_Click(object sender, EventArgs e)方法即可,这是单击按钮的时候执行的事件。希望按一次,则Session["times"]自动加一!现在是点击第一次的时候Session["times"]成功加一了,但再点击的时候就没有任何改变了,请问这是为什么啊?
------解决方案--------------------
修改如下
[code=C#]protected void Page_Load(object sender, EventArgs e)
       {
           if (!IsPostBack)
           {
  Session["times"] = DateTime.Now.Date.ToString();
           }
       
       }
------解决方案--------------------
加if (!IsPostBack)
 {
  Session["times"] = DateTime.Now.Date.ToString();
}
断点,单步