日期:2014-05-18  浏览次数:20765 次

为什么程序刚运行的时候,第一次点击“确定”或“取消”,程序都会执行下一条语句的?
各位朋友,WEB页面有一个Button按钮,代码如下:当第一次点击该按钮的时候,无论你是点击“确定”或“取消”,页面都会出现"用户点击了确定!",为什么会这样的呢?下面的代码到底错在哪里,请各位朋友赐教,谢谢!!!

  protected void Button1_Click(object sender, EventArgs e)
  {
  Button1.Attributes["onclick"] = "return confirm('你确定要删除吗?')";
  Response.Write("用户点击了确定!");
  }

------解决方案--------------------
同一个按钮怎么会同时有"确定"和"取消"呢?
------解决方案--------------------
protected void Button1_Click(object sender, EventArgs e) 

Button1.Attributes["onclick"] = "return confirm( '你确定要删除吗? ')"; 
Response.Write("用户点击了确定!"); 
}

点击按钮,程序执行的顺序是先把服务器部分程序执行完,输出了静态文件到客户端然后在执行javascript代码

看你的代码明显 Response.Write("用户点击了确定!"); 这句执行的顺序高罗,你的javascript还没执行就被输出了字符串了,说的罗嗦希望你能明白
------解决方案--------------------
according to page life cycle,you should regist control attributes since page_load,
on the other hand, when you click button1, Button1_Click will always be executed. just have a try
------解决方案--------------------
protected void Page_Load(object sender, EventArgs e)
{
Button1.Attributes.Add("onclick", "return confirm('确定删除?')");
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("终于执行到这里了!");
}