2011-09-07 64 views
0

这里是我的代码Javascript在asp.net web应用程序中确认框重定向?

Type cstype = this.GetType(); 
      ClientScriptManager cs = this.ClientScript; 
      if (!cs.IsStartupScriptRegistered(cstype, "Script")) 
      { 
       String csScriptText = "confirm('Are you sure you want to leave the page');document.location='Default.aspx'"; 
       cs.RegisterStartupScript(cstype, "TestScript", csScriptText, true); 

      } 

我米试图实现我的web应用程序的confirmbox当用户点击signout按钮,出现......与选择确定,上述取消code.with上面的代码当用户点击confirmmbox上的“OK”时,我可以重定向到默认页面。当用户点击confirmmbox上的“取消”按钮时,我想保持在同一页面上。我该怎么做?

回答

0

只需向重定向添加条件即可。

Type cstype = this.GetType(); 
      ClientScriptManager cs = this.ClientScript; 
      if (!cs.IsStartupScriptRegistered(cstype, "Script")) 
      { 
       String csScriptText = "if(confirm('Are you sure you want to leave the page')) {document.location='Default.aspx'}"; 
       cs.RegisterStartupScript(cstype, "TestScript", csScriptText, true); 

      } 
+0

我也尝试添加条件,但仍任你点击“OK”或“取消”其重定向到默认当我点击确认框的“取消”时,我想保留在同一页面,当“ok”被点击时重定向到默认页面。 – Macnique

3

的确认函数返回一个boolean值,指示用户是否选择行取消。因此,所有你需要做的是重定向仅如果这个函数返回true:

String csScriptText = "if (confirm('Are you sure you want to leave the page')){document.location='Default.aspx';}"; 
+0

我尝试添加条件但仍然要么您点击“ok”或“cancel “将其重定向到默认页面。当”取消“确认框被击中时,我想保留在同一页面中,并且当”ok“被击中时重定向到默认页面 – Macnique

0
if (confirm('some text here')) { 
    window.location = "Default.aspx"; 
} 
相关问题