为什么这样使用Button的事件会被执行两次?

sy246 2005-06-28 01:28:05
页面:
function checkUser()
{
if(document.Form1.all.txtUser.value == "")
{
alert('注册号不能为空!');
document.Form1.all.txtUser.focus();
document.Form1.all.txtUser.select();
return;
}

if(document.Form1.all.txtPwd.value == "")
{
alert('密码不能为空!');
document.Form1.all.txtPwd.focus();
document.Form1.all.txtPwd.select();
return;
}
document.Form1.all.txtSucess.value = "T";
}
<tr>
<td width="35%" height="25" align="right"> </td>
<td width="60%"><form name="form2" method="post" action="">
<input name="btnOk" type="button" class="Button" onClick="checkUser();" value=" 确定 " id="btnLogin" runat="server"> <img src="" width="13" height="7" alt=""> <input name="btnCancel" type="button" class="Button" value=" 取消 " onclick='closeWin();'>
</td>
</tr>

后台:
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnLogin.ServerClick += new System.EventHandler(this.btnLogin_ServerClick);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion

private void btnLogin_ServerClick(object sender, System.EventArgs e)
{
if(txtSucess.Value=="")
{
return;
}
else
{
AccountControl control = new WebApp.AccountControl();
string msg = control.ProcessLogin(this.txtUser.Value,this.txtEmploy.Value,this.txtPwd.Value); if(msg!="")
{
this.passTxt.Value=msg;
}
else
{
Response.Redirect("myLogin.aspx");
}
}
}
...全文
376 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
ghchen 2005-06-29
  • 打赏
  • 举报
回复
楼主怎么会用两个form 看看是不是嵌套
sy246 2005-06-29
  • 打赏
  • 举报
回复
狂晕……
brando_beat 2005-06-29
  • 打赏
  • 举报
回复
呵呵,同上,你调试看看
aaron_lly 2005-06-29
  • 打赏
  • 举报
回复
在page_load中加个回传判断。。。
ye_zi 2005-06-29
  • 打赏
  • 举报
回复
狂晕!接分
龍过鸡年 2005-06-29
  • 打赏
  • 举报
回复
点击按钮的时候, 首先执行的是 onlick js 写的 checkUser
而后开始因 btnLogin_ServerClick .net 写的方法 刷新页面 导致页面方法被执行2遍。
shilei831115 2005-06-29
  • 打赏
  • 举报
回复
ding
jerry_yuan 2005-06-29
  • 打赏
  • 举报
回复
if(document.Form1.all.txtUser.value == "")
{
alert('注册号不能为空!');
document.Form1.all.txtUser.focus();
document.Form1.all.txtUser.select();
return false;
}

if(document.Form1.all.txtPwd.value == "")
{
alert('密码不能为空!');
document.Form1.all.txtPwd.focus();
document.Form1.all.txtPwd.select();
return false;
}
//return时加个false
阿飞666888 2005-06-28
  • 打赏
  • 举报
回复
'
hackate 2005-06-28
  • 打赏
  • 举报
回复
楼上:怎么查看生成后的源文件?
===========
你右激你的右边VS。NET项目文件名。然后选择“在浏览器中察看”
然后等看到页面出来了后。在也面上点鼠标右键,有个“查看原”,那就是原代码了
sy246 2005-06-28
  • 打赏
  • 举报
回复
楼上:怎么查看生成后的源文件?
sy246 2005-06-28
  • 打赏
  • 举报
回复
不可能提示两个呀!我有return语句呀
chenqianliang 2005-06-28
  • 打赏
  • 举报
回复
我想这个问题应该跟asp.net中的自动维护有关。以下是我的理解,你可以通过查看生成后的源文件来证实一下。(我这里未做测试)

因为你的按钮是一个服务器控件,所以asp.net为了能在服务器端捕获到用户在客户端是否有点击此按钮,会自动生成一段代码,每当用户点击一次后,就会把相关的状态值保存到一个隐藏域里面,然后返回服务器后再根据里面的值来执行。
用户每点一次按钮,都会往里面写值 ,但不会立即回传,点击多次后再回传可能会出现执行多次的情况 。
hackate 2005-06-28
  • 打赏
  • 举报
回复
是否是楼上说的。。
就是说你用户名和密码都没填,当然就跳出两个,一提示用户名为空,一个提示密码为空罗
SimpleDay 2005-06-28
  • 打赏
  • 举报
回复
if(document.Form1.all.txtUser.value == "")
{
alert('注册号不能为空!');
document.Form1.all.txtUser.focus();
document.Form1.all.txtUser.select();
return;
}

if(document.Form1.all.txtPwd.value == "")
{
alert('密码不能为空!');
document.Form1.all.txtPwd.focus();
document.Form1.all.txtPwd.select();
return;
}
如果都是空的话。。不是跳两次alert吗?
sy246 2005-06-28
  • 打赏
  • 举报
回复
有AutoEventWireup="false"这句话
fanruinet 2005-06-28
  • 打赏
  • 举报
回复
看看aspx文件的<%@Page里面有没有AutoEventWireup="false"这句话,没有的话添上
renyu732 2005-06-28
  • 打赏
  • 举报
回复
gz

62,041

社区成员

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

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

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

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