Asp.net网络表单显示警报和重定向 [英] Asp.net Webform Display Alert and redirect

查看:148
本文介绍了Asp.net网络表单显示警报和重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前卡住。我有与注册或保存记录的一个按钮一个WebForm。
我想是有它显示一个JavaScript警告,然后重定向到一个页面。
这里是code我使用

I'm currently stuck. I have a webform with a button that registers or saves a record. What i'd like to is have it display a javascript alert and then redirect to a page. Here is the code i am using

protected void Save(..)
{   
    // Do save stuff
    DisplayAlert("The changes were saved Successfully");
    Response.Redirect("Default.aspx");
}

这code只是重定向没有给成功保存的提示。

This code just redirects without giving the prompt Saved Successfully.

下面是我的DisplayAlert code

Here is my DisplayAlert Code

 protected virtual void DisplayAlert(string message)
    {
        ClientScript.RegisterStartupScript(
                        this.GetType(),
                        Guid.NewGuid().ToString(),
                        string.Format("alert('{0}');", message.Replace("'", @"\'").Replace("\n", "\\n").Replace("\r", "\\r")),
                        true
                    );
    }

谁能帮我找到一个解决的办法?

Can anyone help me find a solution to this?

感谢

推荐答案

您不能做一个Response.Redirect的,因为你的JavaScript警告将永远不会显示。最好有你的JavaScript code实际上做了 windows.location.href ='Default.aspx的'显示警报后做重定向。事情是这样的:

You can't do a Response.Redirect because your javascript alert will never get displayed. Better to have your javascript code actually do a windows.location.href='default.aspx' to do the redirection after the alert is displayed. Something like this:

protected virtual void DisplayAlert(string message)
{
    ClientScript.RegisterStartupScript(
      this.GetType(),
      Guid.NewGuid().ToString(),
      string.Format("alert('{0}');window.location.href = 'default.aspx'", 
        message.Replace("'", @"\'").Replace("\n", "\\n").Replace("\r", "\\r")),
        true);
}

这篇关于Asp.net网络表单显示警报和重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆