在asp.net中阻止浏览器上的javascript按钮

时间:2015-12-01 09:23:51

标签: javascript jquery asp.net

我从服务器端调用了一个javascript代码。它工作正常,但现在一旦用户使用浏览器后退按钮进入我的页面,就会再次调用Javascript代码(ieScriptManager.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也很受欢迎。

1 个答案:

答案 0 :(得分:0)

更改page_load事件处理程序中的以下代码块,如下所示:

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

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

相关问题