Page_Load触发无限次

本文关键字:无限 Load Page | 更新日期: 2023-09-27 18:11:22

我的iframe asp.net页面有问题。浏览器url包含我需要在我的iframe页面中使用的参数。显然,我无法通过。net访问所以我想到了在Page_Load方法的末尾添加如下内容:

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

在快捷方式中,我使用Javascript来获取我需要的值并触发reload(),但更改了QueryString。

Page_Load再次触发,现在我有bool isReloaded填充true。条件(!isReloaded)阻止了这次javascript不会被添加到Response中。我不知道为什么,但是Page_Load再次触发,这次没有添加参数,所以它的情况与开始时相同,再次添加JS等。

结果是Page_load无休止地触发。我做错了什么?原因是什么?

Page_Load触发无限次

如果你看一下你的代码,你有这一行:

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

你正在检查location.href的'reloaded' var,但请注意,你的页面被重新加载,只要你改变位置,你的脚本继续执行之前完成,所以它导致页面的重新加载一遍又一遍没有查询字符串。

删除这一行,它应该可以正常工作。

另一件事,虽然,我改变了你的代码一点点注册页面上的脚本,而不是响应。写,

应该没有任何区别,但如果你的代码仍然不能工作,那么试试我的版本:

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

谢谢你的帮助。现在我有了这样的东西,它就好了。

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("if(window.location.href.indexOf('reloaded=true') == -1) window.location.href = window.location.href + '?reloaded=true&contentId=' + last;");
js.Append("<" + "/script>");

我不知道编辑位置执行自动加载;)再次感谢