1. 程式人生 > >輸出對話方塊messageBox

輸出對話方塊messageBox


using System;
using System.Text;

namespace Sp //可以修改成實際專案的名稱空間名稱
{
 /// <summary>
 /// 顯示訊息提示對話方塊。
 /// 李天平
 /// 2005.10.1
 /// </summary>
 public class MessageBox
 {  
  private  MessageBox()
  {   
  }

  /// <summary>
  /// 顯示訊息提示對話方塊
  /// </summary>
  /// <param name="page">當前頁面指標,一般為this</param>
  /// <param name="msg">提示資訊</param>
  public static void  Show(System.Web.UI.Page page,string msg)
  {
   page.RegisterStartupScript("message","<script language='javascript' defer>alert('"+msg.ToString()+"');</script>");
  }

  /// <summary>
  /// 控制元件點選訊息確認提示框
  /// </summary>
  /// <param name="page">當前頁面指標,一般為this</param>
  /// <param name="msg">提示資訊</param>
  public static void  ShowConfirm(System.Web.UI.WebControls.WebControl Control,string msg)
  {
   //Control.Attributes.Add("onClick","if (!window.confirm('"+msg+"')){return false;}");
   Control.Attributes.Add("onclick", "return confirm('" + msg + "');") ;
  }

  /// <summary>
  /// 顯示訊息提示對話方塊,並進行頁面跳轉
  /// </summary>
  /// <param name="page">當前頁面指標,一般為this</param>
  /// <param name="msg">提示資訊</param>
  /// <param name="url">跳轉的目標URL</param>
  public static void ShowAndRedirect(System.Web.UI.Page page,string msg,string url)
  {
   StringBuilder Builder=new StringBuilder();
   Builder.Append("<script language='javascript' defer>");
   Builder.AppendFormat("alert('{0}');",msg);
   Builder.AppendFormat("top.location.href='{0}'",url);
   Builder.Append("</script>");
   page.RegisterStartupScript("message",Builder.ToString());

  }
  public static void ShowConfirmAndRedirect(System.Web.UI.Page page,string msg,string url)
  {
   StringBuilder Builder=new StringBuilder();
   Builder.Append("<script language='javascript' defer>");
   Builder.AppendFormat("return confirm(('{0}');",msg);
   Builder.AppendFormat("top.location.href='{0}'",url);
   Builder.Append("</script>");
   page.RegisterStartupScript("message",Builder.ToString());

  }
  /// <summary>
  /// 輸出自定義指令碼資訊
  /// </summary>
  /// <param name="page">當前頁面指標,一般為this</param>
  /// <param name="script">輸出指令碼</param>
  public static void ResponseScript(System.Web.UI.Page page,string script)
  {
   page.RegisterStartupScript("message","<script language='javascript' defer>"+script+"</script>");
  }

 }
}
Response.Write("<script language=javascript>alert('非法登陸!');</script>");
 Response.Write("<script language=javascript>top.location.href='agree.aspx';</script>");連結
string url = "List.aspx?Spcs=" + lb + "&Ser=" + gjz;
        Response.Write("<script language=javascript>top.location.href='" + url + "';</script>");連結

/// <summary>
    /// 判斷資料庫更新成功或是失敗(孔羸2009.12.10)
    /// </summary>
    /// <param name="row">返回影響的行數</param>
    /// <param name="str1">成功對話方塊提示語句</param>
    /// <param name="str2">失敗對話方塊提示語句</param>
    public static void Message(System.Web.UI.Page page, int row, string str1, string str2)
    {
        try
        {

            if (row > 0)
            {
               
               page.RegisterStartupScript( "alert", "<script>alert('" + str1 + "')</script>");
            }
            else
                page.RegisterStartupScript("alert", "<script>alert('" + str2 + "')</script>");
        }
        catch (Exception ex)
        {
            page.RegisterStartupScript( "alert", "<script>alert('" + ex.Message.ToString() + "')</script>");
        }
        finally
        { }

    }
呼叫時
int row= Db.ExecuteSql(updateSql);
       Db.Message(this.Page,row, "更新資料成功", "更新資料失敗");