简体   繁体   中英

After clicking ok button, do page redirect

below is my WebMsgBox class. I use it to print some message.

using System;
using Microsoft.VisualBasic;
using System.Text;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

/// <summary>
/// Summary description for WebMsgBox
/// </summary>

public class WebMsgBox
{

    protected static Hashtable handlerPages = new Hashtable();

    private WebMsgBox()
    {

    }



    public static void Show(string Message)
    {

        if (!(handlerPages.Contains(HttpContext.Current.Handler)))
        {

            Page currentPage = (Page)HttpContext.Current.Handler;

            if (!((currentPage == null)))
            {

                Queue messageQueue = new Queue();

                messageQueue.Enqueue(Message);

                handlerPages.Add(HttpContext.Current.Handler, messageQueue);

                currentPage.Unload += new EventHandler(CurrentPageUnload);

            }

        }

        else
        {

            Queue queue = ((Queue)(handlerPages[HttpContext.Current.Handler]));

            queue.Enqueue(Message);

        }

    }



    private static void CurrentPageUnload(object sender, EventArgs e)
    {

        Queue queue = ((Queue)(handlerPages[HttpContext.Current.Handler]));

        if (queue != null)
        {

            StringBuilder builder = new StringBuilder();

            int iMsgCount = queue.Count;

            builder.Append("<script language='javascript'>");

            string sMsg;

            while ((iMsgCount > 0))
            {

                iMsgCount = iMsgCount - 1;

                sMsg = System.Convert.ToString(queue.Dequeue());

                sMsg = sMsg.Replace("\"", "'");

                builder.Append("alert( \"" + sMsg + "\" );");

            }

            builder.Append("</script>");

            handlerPages.Remove(HttpContext.Current.Handler);

            HttpContext.Current.Response.Write(builder.ToString());

        }

    }

}

when I use this class's method Show like below

WebMsgBox.Show("some message");

it works fine but when I do somthing like below

WebMsgBox.Show("some message");
Response.Redirect("myform.aspx");

then it doesn't show the message. I need it like when this code executes, first it show the message and after when I click the ok button (which is on the message box), then it should redirects to the myform.aspx.

I don't want to change, this class's code, B'Cos I use this class for many forms, I can't change this class's code just for one form.

How can I do that.

You have to redirect client side:

   builder.Append("alert( \"" + sMsg + "\" );");
   builder.Append("window.location = 'myform.aspx';");

Response.Redirect redirect your page without display it!

You might add an optional paramiter to your function

using System;
using Microsoft.VisualBasic;
using System.Text;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

/// <summary>
/// Summary description for WebMsgBox
/// </summary>

public class WebMsgBox
{

    protected static Hashtable handlerPages = new Hashtable();

    protected static string redirectUrl = "";

    private WebMsgBox()
    {

    }


    public static void Show(string Message, string redirectUrl = "")
    {

        if (!(handlerPages.Contains(HttpContext.Current.Handler)))
        {

            Page currentPage = (Page)HttpContext.Current.Handler;

            if (!((currentPage == null)))
            {
                if (!string.IsNullOrWhiteSpace(redirectUrl))
                    WebMsgBox.redirectUrl = redirectUrl;

                Queue messageQueue = new Queue();

                messageQueue.Enqueue(Message);

                handlerPages.Add(HttpContext.Current.Handler, messageQueue);

                currentPage.Unload += new EventHandler(CurrentPageUnload);

            }

        }

        else
        {

            Queue queue = ((Queue)(handlerPages[HttpContext.Current.Handler]));

            queue.Enqueue(Message);

        }

    }



    private static void CurrentPageUnload(object sender, EventArgs e)
    {

        Queue queue = ((Queue)(handlerPages[HttpContext.Current.Handler]));

        if (queue != null)
        {

            StringBuilder builder = new StringBuilder();

            int iMsgCount = queue.Count;

            builder.Append("<script language='javascript'>");

            string sMsg;

            while ((iMsgCount > 0))
            {

                iMsgCount = iMsgCount - 1;

                sMsg = System.Convert.ToString(queue.Dequeue());

                sMsg = sMsg.Replace("\"", "'");

                builder.Append("alert( \"" + sMsg + "\" );");

            }

            if (!string.IsNullOrWhiteSpace(WebMsgBox.redirectUrl))
            {
                builder.Append("window.location = '" + WebMsgBox.redirectUrl + "'");
                WebMsgBox.redirectUrl = "";
            }

            builder.Append("</script>");

            handlerPages.Remove(HttpContext.Current.Handler);

            HttpContext.Current.Response.Write(builder.ToString());

        }

    }

}

Add this to your class:

protected static Hashtable contexts = new Hashtable();

Change the Show method's definition:

public static void Show(string Message, string redirect) 
{
   // ...
   messageQueue.Enqueue(Message);

   handlerPages.Add(HttpContext.Current.Handler, messageQueue);
   contexts.Add(HttpContext.Current.Handler, redirect); // add this line

   currentPage.Unload += new EventHandler(CurrentPageUnload);
   // ...
}

Change the CurrentPageUnload :

string redirect= contexts[HttpContext.Current.Handler].ToString(); // add this line
// ...
builder.Append("alert( \"" + sMsg + "\" );");
builder.Append("window.location = '" + redirect + "';"); // add this line

Call it like this:

WebMsgBox.Show("some message","myform.aspx");

JavaScript alert function has no callback function which will be executed when you click ok button. Either you want to redirect from Javascript itself and add below code right after your alert message.

window.location = 'yourform.aspx"';

OR

you can use Jquery UI dialog , which executes your code on button click. You can also check out jAlert plugin which is archived now but you can use modified version as mentioned here

I wanted to do this too. I've tried different methods but none of them worked. Here is the solution and worked 100% for me.

Server.Transfer("yourPageName.aspx");

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