Asp.Net C# MessageBox通用類

using System;
using System.Web.UI;
using System.Text;

/// <summary>
/// MessageBox 的摘要說明。
/// </summary>
public class MessageBox
{
    public MessageBox()
    {
        //
        // TODO: 在此處添加構造函數邏輯
        //
    }
    /// <summary>
    /// 一個含有“確定”、“取消”的警告框
    /// </summary>
    /// <param name="_Msg">警告字串</param>
    /// <param name="URL">“確定”以後要轉到預設網址</param>
    /// <returns>警告框JS</returns>
    public static void MsgBox1(string _Msg, string URL)
    {
        string StrScript;
        StrScript = ("<script language=javascript>");
        StrScript += "var retValue=window.confirm('" + _Msg + "');" + "if(retValue){window.location='" + URL + "';}";
        StrScript += ("</script>");
        System.Web.HttpContext.Current.Response.Write(StrScript);

    }
    #region 消息提示 調用方法:  MessageBox.Show()
    /// <summary>
    /// VS2008中適用
    /// 消息提示
    /// 調用方法:  MessageBox.Show(this, "是否可以呢!");
    /// </summary>
    /// <param name="this0">參數:this </param>
    /// <param name="str">參數:顯示文本內容 </param>
    public static void Show(Page this0, string str)
    {
        StringBuilder sb = new StringBuilder("");
        sb.Append("alert('" + str + "');");
        ClientScriptManager cs = this0.ClientScript;
        if (!cs.IsStartupScriptRegistered(this0.GetType(), "Show"))
        {
            cs.RegisterStartupScript(this0.GetType(), "Show", sb.ToString(), true);
        }
    }
   
    /// <summary>
    /// VS2005 AJAX項目中可適應
    /// 用於AJAX項目頁面
    /// </summary>
    /// <param name="control">調用控件 Button1</param>
    /// <param name="type">調用控件類型 Button1.GetType()</param>
    /// <param name="str">提示信息字符串</param>
    public static void Show(Control control, Type type, string str)
    {
        StringBuilder sb = new StringBuilder("");
        sb.Append("alert('" + str + "');");
        //System.Web.UI.ScriptManager.RegisterStartupScript(control, type, "show", sb.ToString(), true);
    }
    #endregion

    #region 打開新頁面 調用方法: MessageBox.OpenUrl()
    /// <summary>
    /// VS2008中適用
    /// 打開新頁面
    /// </summary>
    /// <param name="this0">參數:this</param>
    /// <param name="url">參數:新頁面地址</param>
    /// <param name="target">打開頁面的方式</param>
    public static void OpenUrl(Page this0, string url, string target)
    {
        StringBuilder sb = new StringBuilder("");
        sb.Append("window.open('" + url + "','" + target + "');");
        ClientScriptManager cs = this0.ClientScript;
        if (!cs.IsStartupScriptRegistered(this0.GetType(), "OpenUrl"))
        {
            cs.RegisterStartupScript(this0.GetType(), "OpenUrl", sb.ToString(), true);
        }      
    }
    #endregion

    #region 彈出消息提示,並且打開新頁面 MessageBox.ShowUrl()
    /// <summary>
    /// 彈出消息提示,並且打開新頁面
    /// </summary>
    /// <param name="page0">參數:Page</param>
    /// <param name="this0">參數:this</param>
    /// <param name="str">參數:消息提示文本內容</param>
    /// <param name="url">參數:新頁面地址</param>
    public static void ShowUrl(Page this0, string str, string url)
    {
        StringBuilder sb = new StringBuilder("");
        sb.Append("alert('" + str + "');");
        sb.Append("document.location.href='" + url + "';");   
        ClientScriptManager cs = this0.ClientScript;
        if (!cs.IsStartupScriptRegistered(this0.GetType(), "ShowUrl"))
        {
            cs.RegisterStartupScript(this0.GetType(), "ShowUrl", sb.ToString(), true);
        }
    }
    #endregion

    #region 彈出詢問消息提示,選擇後打開新頁面 MessageBox.ShowConfirmURL()
    /// <summary>
    /// 彈出詢問消息提示,並且打開新頁面
    /// </summary>
    /// <param name="this0">參數:this</param>
    /// <param name="str">參數:消息提示文本內容</param>
    /// <param name="urlYes">參數:點選"確定"後導航的頁面地址</param>
    /// <param name="urlNo">參數:點選"取消"後導航的頁面地址</param>
    public static void ShowConfirmURL(Page this0, string str, string urlYes, string urlNo)
    {
        StringBuilder sb = new StringBuilder("");
        sb.Append("if (window.confirm('" + str + "'))");
        sb.Append("{");
        if (urlYes != "")
        {
            sb.Append("  document.location.href='" + urlYes + "';");
        }
        else
        {
            sb.Append("  true;");
        }
        sb.Append("}");
        sb.Append("else");
        sb.Append("{");
        if (urlNo != "")
        {
            sb.Append("  document.location.href='" + urlNo + "';");
        }
        else
        {
            sb.Append("  false;");
        }
        sb.Append("}");
        ClientScriptManager cs = this0.ClientScript;
        if (!cs.IsStartupScriptRegistered(this0.GetType(), "ShowUrl"))
        {
            cs.RegisterStartupScript(this0.GetType(), "ShowUrl", sb.ToString(), true);
        }
  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章