Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用C显示消息后重定向页面#_C#_Asp.net - Fatal编程技术网

C# 使用C显示消息后重定向页面#

C# 使用C显示消息后重定向页面#,c#,asp.net,C#,Asp.net,我只是尝试先显示消息,然后将用户重定向到另一个页面。我遇到的问题是,它没有首先显示消息,但它将页面重定向到正确的方向。 这是我的密码 if (some condition == true) { string message = string.Empty; message = "Success. Please check your email. Thanks."; ClientScript.RegisterStartupScript(GetType(), "alert"

我只是尝试先显示消息,然后将用户重定向到另一个页面。我遇到的问题是,它没有首先显示消息,但它将页面重定向到正确的方向。 这是我的密码

if (some condition == true)
{
    string message = string.Empty;
    message = "Success.  Please check your email.  Thanks.";
    ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + message + "');", true);

    Response.Redirect("Login.aspx");    
}

这是因为代码从服务器端重定向,甚至在脚本到达客户端浏览器之前。您应该删除该重定向并修改javascript,以便在显示该消息后在客户端完成重定向


编辑:你一定要检查一下。

这里的问题是你在做两件事:

  • 将脚本添加到发送到显示JavaScript警报的浏览器的输出中
  • 用来触发一个事件
  • 后者(2)意味着(1)实际上什么都不做。要在此处实现所需功能,可以使用RegisterStartupScript向下发送脚本,如:

    alert('Message');
    window.location.href = 'login.aspx';
    
    因此,您需要删除
    响应。重定向
    行并使用:

    ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + message + "'); window.location.href = 'login.aspx'", true);
    

    实现结果的最佳方法是使用Javascript(客户端)异步执行

    如果您希望在服务器端执行,下面是一个示例:

    protected void btnRedirect_Click(object sender, EventArgs e)
    {
        string message = "You will now be redirected to YOUR Page.";
        string url = "http://www.yourpage.com/";
        string script = "window.onload = function(){ alert('";
        script += message;
        script += "');";
        script += "window.location = '";
        script += url;
        script += "'; }";
        ClientScript.RegisterStartupScript(this.GetType(), "Redirect", script, true);
    }
    
    你可以在C#中使用计时器。只需给用户足够的时间阅读消息,然后将用户重定向到所需页面


    此线程在使用计时器方面有一个很好的示例:

    这是否回答了您的问题?