简体   繁体   English

页面重新加载后从 JS 的按钮单击仅有效并不一致

[英]Button Click from JS after page reload only works does not work consistently

I have an ASP.Net web app and a part I'm working on is made up three pages:我有一个 ASP.Net Web 应用程序,我正在处理的部分由三个页面组成:

Main Page, Search User Page and Edit User Page.主页、搜索用户页面和编辑用户页面。

The workflow is as follows:工作流程如下:

  • Login and go to the Main Page.登录并转到主页。 (Works Fine!) (工作正常!)
  • Go to Search User Page and search for users based on some criteria.转到搜索用户页面并根据某些条件搜索用户。 (Works Fine!) (工作正常!)
  • Click on a user on the search result table and go to the Edit User Page with the user info populated.单击搜索结果表上的用户,然后转到填充了用户信息的编辑用户页面。 (Works Fine!) (工作正常!)
  • Press a "Save and Go Back" button and go back to the search results.按“保存并返回”按钮并返回到搜索结果。 The expected behavior is for the search page to reload the search results (and hence if the user attributes are changed in a way that they do not meet the search criteria they are not displayed. (This works Funky!)预期的行为是搜索页面重新加载搜索结果(因此,如果用户属性以不符合搜索条件的方式更改,则不会显示它们。 (这很有趣!)

This is how the logic works:这是逻辑的工作原理:

In EditUserInfo.aspx.cs I have:EditUserInfo.aspx.cs我有:

 public partial class EditUserInfo: basePage{

    protected void Page_Load(object sender, EventArgs e) {

    }

    protected void btnSaveAndGoBack_Click(object sender, EventArgs e) {
        //Save/Update User Data
        Session["SearchAfterLoad"] = true;
        ClientScript.RegisterStartupScript("string".GetType(), "goBack", "<script type=\"\"text/javascript\"\" language=\"\"javascript\"\">window.history.go(-2);</script>");
    }

In SearchUsersPage.aspx.cs I have:SearchUsersPage.aspx.cs我有:

 public partial class SearchUsersPage: basePage{

    public string SearchAfterLoad{
        get {
            if (Session["SearchAfterLoad"] != null) { return Session["SearchAfterLoad"].ToString();}
            else { return String.Empty; }
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e) {

    }
    protected void btnSearch_Click(object sender, EventArgs e) {
         /*all the logic to use the UI elements to 
           search and populate the users in a table*/
         Session["SearchAfterLoad"] = false;
    }

in SearchUsersPage.aspx I have:SearchUsersPage.aspx我有:

<script type="text/javascript">
$(document).ready(function () {

    var clickButton = document.getElementById("<%= btnSearch.ClientID %>");
    if ("<%=SearchAfterLoad%>" == "True") {
        clickButton.click();
    }
});
</script>

My issue is: When debugging through Visual Studio (2019, 16.4.0), the logic works perfectly.我的问题是:通过 Visual Studio (2019, 16.4.0) 调试时,逻辑运行良好。 However!然而! When I open the browser and navigate to my localhost (running on local iis) to test this, it will NOT click that search button and the search results do not get updated.当我打开浏览器并导航到我的本地主机(在本地 iis 上运行)进行测试时,它不会单击该搜索按钮并且搜索结果不会更新。

Other observations: This works fine on my colleague's PC with the same windows and the same VS fine no matter if we are debugging or just visiting the localhost.其他观察:这在我同事的 PC 上运行良好,具有相同的窗口和相同的 VS,无论我们是在调试还是只是访问本地主机。 However it does not run when deployed to our test server.但是,它在部署到我们的测试服务器时不会运行。

It seems the first time we navigate to the search page, the if statement in the document.ready() function translates to:似乎我们第一次导航到搜索页面时, document.ready()函数中的if 语句转换为:

if ("" == "True")

and never gets updated/re-rendered to if("True" == "True") when we go back to that page from the edit user page.当我们从编辑用户页面返回该页面时,永远不会更新/重新呈现为if("True" == "True")

PS Session["SearchAfterLoad"] is preserved and not lost when I move back and forth between the pages (but to be honest I have only verified it when in debug mode) PS Session["SearchAfterLoad"]在页面之间来回移动时被保留并且不会丢失(但老实说我只在调试模式下验证过)

Things That I have tried:我尝试过的事情:

  • In the btnSaveAndGoBack_Click() function called the URL directly by putting it into Response.Redirect() or by assigning it to window.location.href but then the search page is loaded fresh with no search criteria populated in its UI (just like visiting it the first time ever)btnSaveAndGoBack_Click()函数中,通过将 URL 放入Response.Redirect()或将其分配给window.location.href来直接调用 URL,但随后搜索页面会重新加载,其 UI 中未填充任何搜索条件(就像访问它一样有史以来第一次)

  • Play with SessionStateServer and ASP.Net Session Manager, These dont have any effect.使用 SessionStateServer 和 ASP.Net Session Manager,这些没有任何效果。


Looking forward:期待:

  • Any good hint as to what the problem could be.关于问题可能是什么的任何好的提示。 Or,或者,

  • Or get the iis/ or whoever in charge to rebuild that page when we go back to it so I get my if("True" == "True") .或者在我们返回时让 iis/ 或负责重建该页面的人得到我的if("True" == "True")

  • For all other fool-proof ways to trigger that button click.对于触发该按钮单击的所有其他万无一失的方法。 Or,或者,

  • Any alternate way of going back and forth between the pages that saves me all the headache!在页面之间来回切换的任何替代方式都可以让我免于头疼!

我的代码中的问题是在分配导致会话变量重置的会话变量后立即调用返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM