【问题标题】:prevent javascript on browser back button in asp.net在 asp.net 中阻止浏览器后退按钮上的 javascript
【发布时间】:2015-12-01 09:23:51
【问题描述】:

我已经从服务器端调用了一个 javascript 代码。它工作正常,但现在一旦用户使用浏览器后退按钮并来到我的页面,就会再次调用 Javascript 代码(即 ScriptManager.RegisterStartupScript)。 我怎样才能防止这种情况发生。我相信页面加载中的某些东西。 有人可以检查并帮助。

下面是我的代码。

 protected void Button1_Click1(object sender, EventArgs e)
    {

        this.ADDMsg.Visible = false;
        string dropdownvalue = HiddenField.Value;
        this.ADDMsg.Text = "";
        bool flag = true;
        try
        {

            if (flag)
            {
                if (this.vpsReq == null)
                {
                    this.vpsReq = new VPSRequest();
                    try
                    {

                        this.Record_Request_VPS(this.vpsReq, dropdownvalue);


                        ScriptManager.RegisterStartupScript(this, this.GetType(), "ShowStatus", "javascript:alert('Request Submitted');", true);


                    }
                    catch (Exception exception2)
                    {
                        this.ADDMsg.Text = exception2.Message;

                    }

                }
                else
                {

                }


            }
            else
            {
                this.ADDMsg.Text = "Please correct the errors above and click <i>Submit</i>.";
            }

        }
        catch (NullReferenceException)
        {
        }

    }  

页面加载为:

 protected void Page_Load(object sender, EventArgs e)
    {

        txtFrom.Attributes.Add("readonly", "readonly");

        if (IsPostBack)
        {
            ClientScript.RegisterHiddenField("isPostBack", "1");
        }



        clnImplDate = txtFrom.Text;
        User = new User();

        try
        {
            User = (User)this.Session["User"];
        }
        catch (Exception)
        {
        }
        if ((User.UserID == null) || (User.UserID == ""))
        {
            User = Utility.GetUserInfo(User);
        }

        this.Session["User"] = User;

    }

另外,ispostback 变量的用法如下所示(为了更清楚,想添加此代码)。

 $(document).ready(function () {

        var isPostBackObject = document.getElementById('isPostBack');
        if (isPostBackObject == null) {
            $('#<%= ADDMsg.ClientID %>').hide();
        }
        $("#" + "ClaimVEBP").hide();
        });

任何解决方案(即使使用 jQuery)都值得赞赏。

【问题讨论】:

    标签: javascript jquery asp.net


    【解决方案1】:

    像这样在 page_load 事件处理程序中更改以下代码块:

    if (!IsPostBack)
    {
       ClientScript.RegisterHiddenField("isPostBack", "1");
    }
    

    !回发 表示 Clientscript 只会在页面第一次加载时运行。

    【讨论】:

    • 感谢您的回复,但问题仍然存在。我使用浏览器返回按钮返回后,弹出“请求已提交”
    猜你喜欢
    • 2013-01-11
    • 2017-06-29
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-07
    • 1970-01-01
    • 2014-04-17
    相关资源
    最近更新 更多