PAGE_LOAD EVENTS FIRES INFINITY TIMES

Ankita Pandey

Total Post:183

Points:1285
Posted by  Ankita Pandey
 940  View(s)
Ratings:
Rate this:

I have problem with my iframe asp.net page. Browser url containst parameter which I need to use in my iframe page. Obviously I can't get access via .NET so I came up 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 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 (!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 so it's the same situation as at the beginning and again is adding JS etc.

Result is that Page_load fires endlessly. What did I do wrong ? What is the reason ?

  1. Royce Roy

    Post:149

    Points:1043
    Re: Page_Load events fires infinity times

    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);

        }

    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!