【问题标题】:HTML onload script triggered too many times?HTML onload 脚本触发了太多次?
【发布时间】:2017-02-11 20:07:21
【问题描述】:

我创建了一个简单的 ASP.Net html 页面。我想进行密码检查,只有在页面最初加载时,我使用脚本进行了检查,并将标签 onload 分配给 body。问题是,每次按下按钮都会触发密码检查。为什么会发生这种情况?如何仅在打开页面时执行密码检查? 提前致谢。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FotoDiClasse.aspx.cs" Inherits="FotoDiClasse" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>Choose your sweatshirt</title>
    <!-- password control -->
    <script>
        var password;
        var pass1 = "1234";
        var firstTime = true;

        function checkPassword()
        {
            if (firstTime)
                firstTime = false;
                password = prompt("Enter password to access the site", '');
                if (password != pass1) window.location = "http://www.google.com";
        }      
    </script>
</head>
<body onload="checkPassword()">
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="CreateButton" runat="server" Text="Create" Width="240px" OnClick="CreateButton_Click" />
        <asp:Button ID="SendButton" runat="server" Text="Send" Width="240px" OnClick="SendButton_Click" />
    </div>
    </form>
</body>
</html>

这个“第一次”标志不起作用

【问题讨论】:

  • 应该注意password = prompt... 将运行无论firstTime 是否为真,因为您没有在if 语句周围使用大括号。仅仅因为你缩进了代码,并不意味着代码不会运行。

标签: html asp.net onload


【解决方案1】:

它被称为PostBack。每次按下按钮时,页面都会执行表单发布 (PostBack)。这意味着页面被重新加载。

使用下面的 sn-p,您只能在页面首次加载时调用 JavaScript 函数。

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        ScriptManager.RegisterStartupScript(Page, Page.GetType(), "runScript", "alert('This is not a PostBack');", true);
    }
}

【讨论】:

  • 在哪里添加?
  • Page_Load 应该出现在PageName.aspx.cs 文件中。你在那里添加它。
  • 谢谢,我也试试这个!! :)
  • 一件事。我应该写什么而不是“runScript”、脚本名称、方法或其他什么?
  • 这只是您为该脚本指定的名称(键),因此您可以稍后在需要时引用它。您可以将其更改为其他内容。
【解决方案2】:

发生这种情况是因为每次按下按钮时都会加载页面并调用 onload() 方法,因此每次都会调用该方法。

为了解决这个问题,您需要设置某种函数来检查您的页面是否是第一次加载的,您可以使用 cookie 来存储该会话。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FotoDiClasse.aspx.cs" Inherits="FotoDiClasse" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>Choose your sweatshirt</title>
    <!-- password control -->
    <script>
        var password;
        var pass1 = "1234";
        var firstTime = true;

        function checkPassword()
        {
            if (firstTime)
                firstTime = false;
                password = prompt("Enter password to access the site", '');
                if (password != pass1) window.location = "http://www.google.com";
        }      
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="CreateButton" runat="server" Text="Create" Width="240px" OnClick="CreateButton_Click" />
        <asp:Button ID="SendButton" runat="server" Text="Send" Width="240px" OnClick="SendButton_Click" onblur="checkPassword()"/>
    </div>
    </form>
</body>
</html>

【讨论】:

  • 对不起,我是 Html 新手。你把它放在一个普通的 aspx 页面的什么地方?
  • 放入脚本标签
  • 我把它贴在主要问题上
  • 你为什么不检查你的密码onblur的文本框??
  • 也许你是对的,我正在这样做。但是以防万一我需要在第一次加载页面时这样做,我应该把你的代码放在哪里?总之非常感谢!! :)
猜你喜欢
  • 1970-01-01
  • 2015-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-23
  • 1970-01-01
  • 2019-06-12
  • 1970-01-01
相关资源
最近更新 更多