Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:-
ASP.NET
<%@ 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 File:-
C#
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
Posted
Updated 2-Mar-17 22:48pm
v3

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;
}
 
Share this answer
 
Comments
Palash Sachan 3-Mar-17 4:58am    
although it reloads the page 1 time but then it stops the reloads and does my work perfectly
thank a lot sir :)
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.
 
Share this answer
 
v2
Comments
Palash Sachan 3-Mar-17 4:40am    
but i need the javascript to load as the page is loaded..On removing this the javascript will not work when the page is loaded..
also i tried to use jQuery and its not loaded when the page is loaded
$(window).load(function () {
document.getElementById('<%= bttn.ClientID %>').click();
});

so is there any other way to stop that reload??
Karthik_Mahalingam 3-Mar-17 4:44am    
what is your requirement.
Palash Sachan 3-Mar-17 4:54am    
my requirment is very small..what here i want to do is-->
"when and as soon as the webform7.aspx page is fully loaded then it should auto click the button which will invoke the code behind method and do the stuff"

I did this only but the page is reloading and i need to stop the reload..
Karthik_Mahalingam 3-Mar-17 4:59am    
ok, you have got a solution.
Palash Sachan 3-Mar-17 5:00am    
yeah..thanks for the help :)
Here is why it happens: onload is set to Show, 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.
 
Share this answer
 
Comments
Palash Sachan 3-Mar-17 4:43am    
okay so how do i stop that autopostback from happening when the button is clicked??
Thomas Daniels 3-Mar-17 5:11am    
I don't know that, but it looks like F-ES Sitecore got a good solution for your problem.
Palash Sachan 3-Mar-17 5:14am    
yeah the problem is solved..now the page is not reloading
anyway, thanks to you also for the help :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900