如何阻止页面无限次自动重新加载? [英] How do I stop the page from reloading automatically infinite times?
问题描述
为什么页面会无限次自动刷新?
页面刷新无限时间就像一个循环..
i不希望页面被刷新..
我该怎么做
请帮助
我尝试了什么:
当页面加载javascript时点击按钮onclick事件就像这样..
ASPX文件: -
Why is the page is refreshing automatically infinite times?
the page is refreshed infinite time like a loop..
i don't want page to be refreshed..
How do i do this
please help
What I have tried:
when the page loads the javascript clicks the button onclick event like this..
ASPX File:-
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm7.aspx.cs" Inherits="Demo.WebForm7" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="../Scripts/jquery-3.1.1.js"></script>
<script type="text/javascript">
function Show() {
document.getElementById('<%= bttn.ClientID %>').click();
}
window.onload = Show;
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
hello<br />
<asp:Button ID="bttn" runat="server" OnClick="bttn_Click" Style="display: none" />
<asp:Label ID="success" runat="server" Text="done" Visible="false" />
</div>
</form>
</body>
</html>
ASPX.CS文件: -
ASPX.CS File:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Demo
{
public partial class WebForm7 : Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void bttn_Click(object sender, EventArgs e)
{
success.Visible = true;
//doing some other work also
}
}
}
请帮我停止这个无限循环页面重新加载
提前感谢
please help me to stop this infinite loop page reload
thanks in advance
推荐答案
在你的剧本周围放一个占位符
Put a placeholder around your script
<asp:Placeholder ID="PlaceScript" runat="server">
<script type="text/javascript">
function Show() {
document.getElementById('<%= bttn.ClientID %>').click();
}
window.onload = Show;
</script>
</asp:Placeholder>
然后在点击按钮时隐藏占位符
then hide the placeholder when the button is clicked
protected void bttn_Click(object sender, EventArgs e)
{
success.Visible = true;
PlaceScript.Visible = false;
}
删除此行
remove this line
window.onload = Show;
这将在页面加载后立即调用按钮单击事件,因此将发生无限循环。
this will invoke the button click event as soon as the page is loaded, hence infinite loop will occur.
以下是它发生的原因:onload
设置为显示
,此方法触发按钮单击。当您单击该按钮时,您的浏览器会向服务器发送请求并向您显示响应,这就是页面重新加载的原因。当页面重新加载时,显示
再次被调用,因此它会一直持续。
我不知道你想要做什么,所以我不能说你现在要做什么,但如果你知道原因,你应该能够解决它。
Here is why it happens:onload
is set toShow
, and this method triggers a button click. And when you click the button, your browser posts a request to the server and shows you the response, this is why the page reloads. And when the page reloads,Show
is called again so it goes on forever.
I don't know what you want to do exactly so I can't say what you have to do now, but if you know the cause, you should be able to resolve it.
这篇关于如何阻止页面无限次自动重新加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!