2011-12-20 47 views
7

我正在使用vs 2010.我需要向用户显示消息并重定向页面。如何在重定向页面之前获取警报消息

我使用下面的行。

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

但我没有收到警报消息,并且该页面被直接重定向。

如何获取警报消息?

回答

23

你的代码是开放的窗口,但你要求一个重定向,下面是一个重定向的例子:

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

你需要写:

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

请注意,我已经删除了脚本标签的最后一个参数真正意味着你不能使用的脚本。

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

0

的最佳方式,如果可能的话,就是把代码放到的OnClientClick。

<asp:Button OnClientClick=" alert('blah?');" runat="server" /> 

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

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

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

1

如果你希望把.CS文件,只是试试这个:

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

如果您正在使用的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); 
-1

你问一个重定向页面,下面是一个重定向的例子:

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

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

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) 
相关问题