1. 程式人生 > >一般處理程式寫彈框及連結跳轉

一般處理程式寫彈框及連結跳轉

請求一般處理程式後,如果驗證結果不正確,需要彈框返回提示

下面為所寫的一個類,供一般處理程式呼叫

using System;
using System.Collections.Generic;
using System.Linq;

using System.Web;

namespace TestSpace
{
    public class Alert
    {
        /// <summary>
        /// 跳轉到登入成功頁面
        /// </summary>
        /// <param name="url">跳轉連結</param>
        /// <param name="message"></param>
        public static void Redirect(string url, string message = "登入成功")
        {
            ResponseScript("window.location = '" + url + "';");
        }

        /// <summary>
        /// 彈框,並返回上一頁
        /// </summary>
        /// <param name="message"></param>
        public static void AlertFailed(string message = "登入失敗")
        {
            ResponseScript("alert('" + message + "');javascript:history.go(-1);");
        }

        /// <summary>
        /// Script指令碼
        /// </summary>
        /// <param name="script"></param>
        public static void ResponseScript(string script)
        {
            HttpContext.Current.Response.Write("<script type=\"text/javascript\">\n//<![CDATA[\n");
            HttpContext.Current.Response.Write(script);
            HttpContext.Current.Response.Write("\n//]]>\n</script>\n");
        }
    }

}

using System;
using System.Web;
using System.Web.SessionState;
namespace TestSpace
{
    /// <summary>
    /// login 的摘要說明
    /// </summary>
    public class login : IHttpHandler, IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            string action = context.Request.HttpMethod.ToUpper();
            if (action!="POST")
            {
                Alert.AlertFailed("URL請求無效");
                return;
            }

           else

            {

                Alert.Redirect("Index.aspx");

            }

        }

    }

}