简体   繁体   中英

How do I show a message before redirect

Im saving an employee to the database

when that succeeded i want to show a message to the user i created a method to do so:

<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>

With the javscript:

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

and the ShowMessage() method in Code-Behind:

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;
}

Now when the Employee is saved i call:

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

to show the message. this works fine but im implementing new pages so now i redirect after Employee is inserted.

My Question: How do i show a message to the user when the Employee is added, before it postback OR how can i show the message on the page the user is redirected to

i edited my function ShowMessage to accept 3 vars

ShowMessage(String message, WarningType type, String redirect)

and updated the method:

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;

So when String redirect is empty it just shows the message

But when its not it shows the message and redirects to the page

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM