2012-04-16 71 views
0

根据我的应用程序中的某些值,我需要显示带有确定和取消的确认框,如果它们单击确定,它应该继续并导航到另一个页面,否则如果它们单击取消,它应该保持在同一页面。使用Page.ClientScript从呼叫获得响应

我遇到的问题是,弹出不显示,除非我添加return语句如下

if (true) 
     { 
     Page.ClientScript.RegisterStartupScript(this.GetType(), 
     "ConfirmBox", "confirm('The numbers selected are not in the directory," 
     + sb.ToString()+ 
     " you wish to continue?');", true); 
         return; 
     } 
    Response.Redirect("~/homepage.aspx"); 

正如你可以看到这是行不通的,因为它永远不会去,因为重定向返回,但没有返回,即使条件为真,它也会跳过弹出窗口。我怎样才能做到这一点?

回答

1

我已经试过你的代码,并且动作与你一样。 我的解决方案提供给您:

<script type="text/javascript"> 
function Redirect() 
{ 
location.href = "~/homepage.aspx"; 
} 
</script> 
<script runat="server"> 
void yourFunc() 
{ 
if (true) 
     { 
     Page.ClientScript.RegisterStartupScript(this.GetType(), 
     "ConfirmBox", "if(confirm('The numbers selected are not in the directory," 
     + sb.ToString()+ 
     " you wish to continue?') == true){Redirect();};", true); 
     } 
} 
</script> 

希望它可以帮助你。

+0

请不要使用您的签名。 http://stackoverflow.com/faq#signatures – Pankaj 2012-04-16 03:11:28

1

我遇到的问题是,弹出不显示,除非我添加 return语句如下一旦Page Life Cycle执行完毕它的所有事件

JavaScript code将被执行。所以,回报在这里没有任何意义。


纠正措施

移动代码(条件和Redirect到另一页)JavaScript函数。在您的RegisterStartupScript中调用函数。

0

这可能是你的灵魂。这是基于来自确认框的用户输入限制导航的示例。

Check here