如何在重定向页面之前获取警报消息

我正在使用vs 2010.我需要向用户显示消息并重定向页面。

我使用下面的行。

ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", " alert('User details saved sucessfully');window.open('frmDisplayUsers.aspx');", true); 

但我没有得到警报消息,页面被直接重定向。

如何获得提醒信息?

您的代码是打开窗口但是您要求重定向,下面是重定向的示例:

 ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('User details saved sucessfully');window.location ='frmDisplayUsers.aspx';", true); 

你需要写:

  ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", " alert('User details saved sucessfully'); window.open('frmDisplayUsers.aspx');", true); 

请注意,我删除了脚本标记作为最后一个参数true表示您不能使用脚本标记。

这段代码对我有用。 如果您有任何问题,请告诉我。 此外,您可以使用setTimeout来延迟打开窗口,这可能不是一个非常糟糕的选择。

如果你想输入.CS文件,试试这个:

 var page = HttpContext.Current.CurrentHandler as Page; ScriptManager.RegisterStartupScript(page, page.GetType(), "alert", "alert('" + msg +"');window.location ='"+ aspx +"';", true); 

如果您正在使用UpdatePanel,那么您必须使用它:它适用于更新面板。

  var message = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize("Bill No. : " + BillNo + " successfully generated."); var script = string.Format("alert({0});window.location ='ChhallanPrint.aspx';", message); ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", script, true); 

密码更新警报消息后重定向到登录页面

 Dim MyScript As String = "alert('Password Updated Successfully!. Please Login Again!'); window.location = '../Login.aspx';" 

如果您的目的是将页面放到最顶层,因为您的页面可能位于框架中,而该框架本身位于框架内。

 Dim MyScript As String = "alert('Password Updated Successfully!. Please Login Again!');window.top.location='../Logout.aspx';" ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "MyScript", MyScript, True) 

如果可能,最好的方法是将代码放入OnClientClick。

  

将它放入startupscript的问题是startupscript是在回发上运行的,而不是实时的。 重定向将在回发之前发生。 (可能)

另一种解决方案当然是将重定向放入startupscript代码中。

页面将回发,脚本将运行,然后它将重定向。

您要求重定向页面,下面是重定向的示例:

  string page = "Login.aspx"; string myStringVariable = "Password Update!"; ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + myStringVariable + "');Response.Redirect('"+ page +"' );", true);