如何在重定向

时间:2017-05-16 09:10:23

标签: javascript c# asp.net

我将员工保存到数据库

当成功时我想向用户显示消息 我创建了一个方法:

<div class="MessagePanelDiv">
    <asp:Panel ID="Message" runat="server" Visible="False">
        <button type="button" class="close" data-dismiss="alert">&times;</button>
        <asp:Label ID="LabelTypeMessage" CssClass="messageType" runat="server" />&nbsp;<asp:Literal ID="LiteralMessage" runat="server" />
    </asp:Panel>
</div>

使用javscript:

 $(document).ready(function () {
     window.setTimeout(function () {
         $(".alert").fadeTo(1500, 0).slideUp(500, function () {
             $(this).remove();
         });
     }, 3000);
 });

和Code-Behind中的ShowMessage()方法:

public void ShowMessage(String message, WarningType type)
{
    //temp var to store the message type
    String MessageType;

    //gets the controls from the page
    Panel PanelMessage = this.FindControl("Message") as Panel;
    Label LiteralTypeMessage = PanelMessage.FindControl("LabelTypeMessage") as Label;
    Literal LiteralMessage = PanelMessage.FindControl("LiteralMessage") as Literal;

    //switch case to properly set the message type
    switch (type.ToString())
    {
        case "Succes":
            MessageType = "Success!";
            break;
        case "Info":
            MessageType = "Info!";
            break;
        case "Warning":
            MessageType = "Waarschuwing!";
            break;
        case "Danger":
            MessageType = "Fout!";
            break;
        default:
            MessageType = "";
            break;
    }

    //sets the message and the type of alert, than displays the message
    LiteralTypeMessage.Text = MessageType;
    LiteralMessage.Text = " " + message;
    PanelMessage.CssClass = String.Format("alert alert-{0} alert-dismissable", type.ToString().ToLower());
    PanelMessage.Attributes.Add("role", "alert");
    PanelMessage.Visible = true;
}

现在,当员工被保存时,我致电:

ShowMessage(GetGlobalResourceObject("Global.errors", "SaveSuccess").ToString(), WarningType.Success);

显示消息。这工作正常但我实现新页面所以现在我插入Employee后重定向。

我的问题:如何在添加员工,回发之前向用户显示消息,或者如何在用户重定向到的页面上显示消息

1 个答案:

答案 0 :(得分:0)

我编辑了我的函数ShowMessage以接受3个变量

ShowMessage(String message, WarningType type, String redirect)

并更新了方法:

LiteralTypeMessage.Text = MessageType;
if (string.IsNullOrWhiteSpace(redirect) || redirect == ""){
        LiteralMessage.Text = " " + message;
    }
    else{
        LiteralMessage.Text = " " + message + ", <script>window.setTimeout(function () { $('.alert').fadeTo(1500, 0).slideUp(500, function() {$(this).remove();});window.location = '" + redirect + "'}, 3000);</script>";
    }

PanelMessage.CssClass = String.Format("alert alert-{0} alert-dismissable", type.ToString().ToLower());
PanelMessage.Attributes.Add("role", "alert");
PanelMessage.Visible = true;

因此,当String重定向为空时,它只显示消息

但是当它不显示消息并重定向到页面时