为何按钮对应的点击事件只执行了一次,再点击就无效了?

小V 2012-10-23 05:21:53
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"]成功加一了,但再点击的时候就没有任何改变了,请问这是为什么啊?
...全文
1451 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
叫什么呢 2012-10-24
  • 打赏
  • 举报
回复
你没判断它回传,所以每次都执行Session["times"] = DateTime.Now.Date.ToString();
可以这样修改
  
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
Session["times"] = DateTime.Now.Date.ToString();
}
Show_Works();

}
我的宣言 2012-10-23
  • 打赏
  • 举报
回复
if (!IsPostBack)
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

加if (!IsPostBack)
{
Session["times"] = DateTime.Now.Date.ToString();
}
断点,单步
[/Quote]
首次加载时候执行Session["times"] = DateTime.Now.Date.ToString();

面执行服务器控件的点击会触发Page_load
lhx527099095 2012-10-23
  • 打赏
  • 举报
回复
if (!IsPostBack)
{
首次在这里执行 回调的不会执行
}
wuyq11 2012-10-23
  • 打赏
  • 举报
回复
加if (!IsPostBack)
{
Session["times"] = DateTime.Now.Date.ToString();
}
断点,单步
  • 打赏
  • 举报
回复
修改如下
[code=C#]protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["times"] = DateTime.Now.Date.ToString();


}


}

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧