---
title: "Page_Load events fires infinity times"  
description: "Page_Load events fires infinity times"  
author: "Anonymous User"  
published: 2014-11-14  
updated: 2014-11-14  
canonical: https://www.mindstick.com/forum/12581/page_load-events-fires-infinity-times  
category: "asp.net"  
tags: ["c#", "javascript"]  
reading_time: 2 minutes  

---

# Page_Load events fires infinity times

I have [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) with my iframe [asp.net](https://www.mindstick.com/articles/934/default-folders-available-inside-the-asp-dot-net-application-folder) page. [Browser](https://www.mindstick.com/blog/155254/which-is-the-best-internet-browser) url containst [parameter](https://www.mindstick.com/blog/450/parameter-class-in-c-sharp) which I need to use in my iframe page. Obviously I can't get [access](https://www.mindstick.com/articles/12994/how-foreigners-can-access-blocked-websites-in-china) via .NET so I [came up](https://answers.mindstick.com/qa/41115/who-came-up-with-the-10-percent-plan-what-did-this-plan-say) with the idea that at the end of the Page_Load method add sth like that :

```
protected void Page_Load(object sender, EventArgs e)    {        if (!IsPostBack)        {            bool isReloaded = Request.QueryString.GetValue<bool>("reloaded");            ContentId = Request.QueryString.GetValue<int>("contentId"); //I need this value            if (!isReloaded)            {                StringBuilder js = new StringBuilder("<script language='javascript'>");                js.Append("var last = window.top.location.href.substring(window.top.location.href.lastIndexOf('/') + 1, window.top.location.href.length); ");                js.Append("window.location.href = window.location.href + '?reloaded=true&contentId=' + last;");                js.Append("if(window.location.href.indexOf('reloaded=true') == -1) window.location.reload();");                js.Append("<" + "/script>");                Response.Write(js.ToString());            }        }    }
```

In shortcut I use [Javascript](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript) to get value I need and fire reload() but with changed QueryString.

Page_Load is firing again and now I have bool isReloaded filled with true. The [condition](https://www.mindstick.com/forum/12711/select-query-with-and-condition) (!isReloaded) blocks that this time javascript will not be added to Response. I don't know why, but Page_Load fires again, this time without added [parameters](https://www.mindstick.com/articles/87/passing-parameters-in-c-sharp) so it's the same situation as at the beginning and again is adding JS etc.

[Result](https://www.mindstick.com/blog/12011/advantages-of-getting-result-oriented-seo-from-an-agency) is that Page_load fires endlessly. What did I do wrong ? What is the reason ?

## Replies

### Reply by Royce Roy

if you have a look at your code, you have this line:\

js.Append("if(window.location.href.indexOf('reloaded=true') == -1) window.location.reload();");

you are checking the location.href for the 'reloaded' var, but note that your page is reloaded as soon as you change the location, and your script keeps executing before it is done, so it results in reloading of the page over an over again without the query string.

remove this line and it should work fine.

another thing though, i changed your code a little bit to register the script on page instead of response.write it,

it shouldnt make any difference, but if your code still doesnt work then try my version:

```
protected void Page_Load(object sender, EventArgs e){    if (!IsPostBack)    {        bool isReloaded;        int ContentId;        bool.TryParse(Request.QueryString["reloaded"],out isReloaded);        int.TryParse(Request.QueryString["contentId"],out ContentId); //I need this value        if (!isReloaded)        {            StringBuilder js = new StringBuilder();            js.Append("var last = window.top.location.href.substring(window.top.location.href.lastIndexOf('/') + 1, window.top.location.href.length); ");            js.Append("window.location.href = window.location.href + '?reloaded=true&contentId=' + last;");            ExecScript(js.ToString());        }    }}void ExecScript(string script){    Page page = HttpContext.Current.CurrentHandler as Page;    if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("AttachedScript"))    {        page.ClientScript.RegisterClientScriptBlock(page.GetType(), "AttachedScript", script, true);    }}
```


---

Original Source: https://www.mindstick.com/forum/12581/page_load-events-fires-infinity-times

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
