C# 如何在重定向页面之前获取警报消息
我用的是VS2010。我需要向用户显示消息并重定向页面 我使用下面的一行C# 如何在重定向页面之前获取警报消息,c#,javascript,asp.net,C#,Javascript,Asp.net,我用的是VS2010。我需要向用户显示消息并重定向页面 我使用下面的一行 ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "<script> alert('User details saved sucessfully');window.open('frmDisplayUsers.aspx');</script>", true); ScriptManager.RegisterStartu
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "<script> alert('User details saved sucessfully');window.open('frmDisplayUsers.aspx');</script>", true);
ScriptManager.RegisterStartupScript(this,this.GetType(),“警报”,“警报('User details saccessfully');window.open('frmDisplayUsers.aspx');”,true);
但我没有收到警告信息,页面被直接重定向
如何获取警报消息?您需要写:
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", " alert('User details saved sucessfully'); window.open('frmDisplayUsers.aspx');", true);
请注意,我已经删除了脚本标记,因为最后一个参数true表示您不能使用脚本标记
这个代码对我有用。如果你有任何问题,请告诉我。此外,您可以使用
setTimeout
延迟窗口打开,这可能不是一个很好的选择。您的代码正在打开窗口,但您请求重定向,下面是重定向的示例:
ScriptManager.RegisterStartupScript(this, this.GetType(),
"alert",
"alert('User details saved sucessfully');window.location ='frmDisplayUsers.aspx';",
true);
string page = "Login.aspx";
string myStringVariable = "Password Update!";
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + myStringVariable + "');Response.Redirect('"+ page +"' );", true);
如果可能,最好的方法是将代码放入OnClientClick
<asp:Button OnClientClick=" alert('blah?');" runat="server" />
将其放入startupscript的问题在于,startupscript是在回发上运行的,而不是实时运行的。重定向将在回发之前发生。(可能)
当然,另一个解决方案是将重定向放入startupscript代码中
页面将发回,脚本将运行,然后重定向 如果要放入.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);
您请求重定向页面,下面是重定向的一个示例:
ScriptManager.RegisterStartupScript(this, this.GetType(),
"alert",
"alert('User details saved sucessfully');window.location ='frmDisplayUsers.aspx';",
true);
string page = "Login.aspx";
string myStringVariable = "Password Update!";
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + myStringVariable + "');Response.Redirect('"+ page +"' );", 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)
我不能成功地使用那些方法。 我改用
Server.Transfer()
示例:(来自)
Response.Write(“警报('User details saved successfully');”;
Server.Transfer(“frmDisplayUser.aspx”);
如果存在会话对象。我相信这种方法会消失。