【发布时间】: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