为什么会弹出两次alert对话框 我在用asp.net 做网页的时候,在一个button 上加了一个判断代码,就是用javascript代码弹出对话框,我用的是Response.Write("<script lang........>alert('要说的话');</"+"script>");但是的出的结果是:点击按钮后弹出一次,但当你点“确定”后,它又弹出一次,再点“确定”后才行!请问各位大虾,这是为什么啊? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 adandelion(被封杀) 这个...我还奇怪了一下,原来是虎人的 正常情况下,没有弹出两次啊你是不是在这里多了两次button注册事件呢#region Web Form Designer generated codeoverride protected void OnInit(EventArgs e){ InitializeComponent(); base.OnInit(e); this.Button1.Click += new System.EventHandler(this.Button1_Click);//这里一次} private void InitializeComponent(){ this.Button1.Click += new System.EventHandler(this.Button1_Click);//这里是不是又多了一次呢? this.Load += new System.EventHandler(this.Page_Load);}#endregion 主要部分while(sdr.Read()) { if(sdr["Pwd"].ToString()!=this.Pwd.Text.ToString().Trim()) { Response.Write("<script language='javascript'>alert('输入的密码错误!');</"+"script>"); } else { this.PassPanel.Visible=true; this.LoginPanel.Visible=false; this.UserNameL.Text=sdr["UserName"].ToString(); this.Grade.Text=sdr["UserGrade"].ToString(); this.LastLoginTime.Text=Convert.ToDateTime(sdr["LastLoginTime"]).ToLongDateString(); } } PageLoad事件里的问题?if(!IsPostBack){ //这里面的只在页面第一次加载的时候才运行.如果不写在IsPostBack里面,每次页面提交时候都运行. Button1.Attributes.Add("onclick","alert('确定');");} while(sdr.Read()){.....}里面的结果不只是一条记录吧,有几条就弹出几条啊~~~(密码不正确的记录) 有可能是回传的问题,还是在if(!IsPostBack)里注册的方法比较好. adandelion(被封杀) 这个...我还奇怪了一下,原来是虎人的--------------??????????????????? while(sdr.Read()){这里面设置断点.调试看看它运行几次?估计是查询的记录不只一条,或者你使用数据库的跟踪,看看SQL执行的代码的结果是产生几条记录.} 我的注册事件: #region Web 窗体设计器生成的代码 override protected void OnInit(EventArgs e) { // // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); } /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器 /// 修改此方法的内容。 /// </summary> private void InitializeComponent() { this.Login.Click += new System.EventHandler(this.Login_Click); this.Cancel.Click += new System.EventHandler(this.Cancel_Click); this.Logout.Click += new System.EventHandler(this.Logout_Click); this.Load += new System.EventHandler(this.Page_Load); } #endregion 首先看看你生成的客户端代码是否有两段上面的js脚本(浏览页面的源文件,应该也页面的最上面)?如果这句话在页面出来过两次,再确定这两次都是那个函数写的?你的代码里只有这一个地方向Response写这段脚本了么?其次你应该使用下面这种方法来注册客户端脚本,这样注册更安全,只要ScriptKeyWord一样的就不会多次注册:this.RegisterStartupScript("ScriptKeyWord","your javascript here."); while(sdr.Read())你使用上述语句进行循环,如果你的数据库里面有两条记录相同(或你查出来的数据有多条),那么,就有可能多次弹出警告框.你可检查一下你所求取的数据是否多条,并且,可使用断点的方式进行判断. 门槛言之有理,另外也可能是页面回送引起的。把你response.write那句话放在if(!Page.IsPostBack){}里 服务器端Page_Load:if (!IsPostBack){ Button1.Attributes.Add("onclick","return _alert();");}客户端:<script language="javascript">function _alert(){ alert("information"); return false;}</script> cuike519(Help you,Help me),你好,我不知道this.RegisterStartupScript的具体用法啊,你能具体说一下吗?它不是直接用的吧? Page.RegisterStartupScript("script1","<script>alert('删除失败!');</script>"); 在你的Buton事件设个断点调试一下,看看是Button事件触发两次,还是里面循环执行两次。 运行完后。查看源文件,看看是不是有两个alert() 你肯定是在循环里面加的java脚本,你只要判断最后一次循环调用java脚本就行了 在HEAD 中增加<base target="_self">试试 应该用if(!IsPostBack){ }避免重复执行 我一用if(!IsPostBack){ }那么点按钮就没反映了 你的sdr是DataReader吗?试一下用DataTable判断看行不? 类似DataTable dt = new DataTable();dt = [绑定数据];if(dt.Rows.Count != 0){if(dt.Rows[0]["Pwd"].ToString()!=this.Pwd.Text.ToString().Trim()){Response.Write("<script language='javascript'>alert('输入的密码错误!');</"+"script>");}else{this.PassPanel.Visible=true;this.LoginPanel.Visible=false;this.UserNameL.Text=dt.Rows[0]["UserName"].ToString();this.Grade.Text=dt.Rows[0]["UserGrade"].ToString();this.LastLoginTime.Text=Convert.ToDateTime(dt.Rows[0]["LastLoginTime"]).ToLongDateString();}} 你是直接写在button的事件里的吗? Visual Studio 命令行工具 sqlhelp报错,求高数指教。。。。。 web站点引用项目类库中的log4net配置问题 “System.Text.Encoding”并不包含“Default”的定义 取地址前面的部分,有没有现成的函数?? 请问毕业设计是搞软件开发的毕业论文该怎么写啊 Asp.net与防火墙的问题 !!!求助:文件上传(异地存储)问题 在问个问题? 编译器错误信息: BC30451: 名称“substring”未声明,why aspnet的cs文件中可以在页面输出服务器端控件么? ==================高分求教远程关机方法===============
你是不是在这里多了两次button注册事件呢
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
this.Button1.Click += new System.EventHandler(this.Button1_Click);//这里一次
}
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);//这里是不是又多了一次呢?
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
while(sdr.Read())
{
if(sdr["Pwd"].ToString()!=this.Pwd.Text.ToString().Trim())
{
Response.Write("<script language='javascript'>alert('输入的密码错误!');</"+"script>");
}
else
{
this.PassPanel.Visible=true;
this.LoginPanel.Visible=false;
this.UserNameL.Text=sdr["UserName"].ToString();
this.Grade.Text=sdr["UserGrade"].ToString();
this.LastLoginTime.Text=Convert.ToDateTime(sdr["LastLoginTime"]).ToLongDateString();
}
}
if(!IsPostBack)
{
//这里面的只在页面第一次加载的时候才运行.如果不写在IsPostBack里面,每次页面提交时候都运行.
Button1.Attributes.Add("onclick","alert('确定');");
}
{
.....
}里面的结果不只是一条记录吧,有几条就弹出几条啊~~~(密码不正确的记录)
里注册的方法比较好.
这个...我还奇怪了一下,原来是虎人的
--------------
???????????????????
{
这里面设置断点.调试看看它运行几次?估计是查询的记录不只一条,或者你使用数据库的跟踪,看看SQL执行的代码的结果是产生几条记录.
}
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Login.Click += new System.EventHandler(this.Login_Click);
this.Cancel.Click += new System.EventHandler(this.Cancel_Click);
this.Logout.Click += new System.EventHandler(this.Logout_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion
如果这句话在页面出来过两次,再确定这两次都是那个函数写的?你的代码里只有这一个地方向Response写这段脚本了么?
其次你应该使用下面这种方法来注册客户端脚本,这样注册更安全,只要ScriptKeyWord一样的就不会多次注册:
this.RegisterStartupScript("ScriptKeyWord","your javascript here.");
你使用上述语句进行循环,如果你的数据库里面有两条记录相同(或你查出来的数据有多条),那么,就有可能多次弹出警告框.
你可检查一下你所求取的数据是否多条,并且,可使用断点的方式进行判断.
if(!Page.IsPostBack)
{}
里
if (!IsPostBack)
{
Button1.Attributes.Add("onclick","return _alert();");
}客户端:
<script language="javascript">
function _alert()
{
alert("information");
return false;
}
</script>
<base target="_self">
试试
{ }
避免重复执行
{ }
那么点按钮就没反映了
DataTable dt = new DataTable();
dt = [绑定数据];
if(dt.Rows.Count != 0)
{
if(dt.Rows[0]["Pwd"].ToString()!=this.Pwd.Text.ToString().Trim())
{
Response.Write("<script language='javascript'>alert('输入的密码错误!');</"+"script>");
}
else
{
this.PassPanel.Visible=true;
this.LoginPanel.Visible=false;
this.UserNameL.Text=dt.Rows[0]["UserName"].ToString();
this.Grade.Text=dt.Rows[0]["UserGrade"].ToString();
this.LastLoginTime.Text=
Convert.ToDateTime(dt.Rows[0]["LastLoginTime"]).ToLongDateString();
}
}