MessageBox一般情況下都會在WinForm中使用,如何在C#中使用,結合平日的開發經驗,也在網上搜尋了一下,進行總結歸納了一下,主要有以下幾種方法:
1.使用JavaScript實現:在aspx檔案中拖入一個button按鈕,雙擊按鈕,編輯該按鈕的事件如下:
protected void msgButton_Click(object sender, EventArgs e)
{
Page.RegisterStartupScript("alert", "<script language='Javascript'> alert('彈出一個訊息框!')</script>");
}
2.利用C#直接實現:
右擊Solution Explore中的網站,點擊“添加引用”將System.Windows.Forms添加進來
然後在aspx檔案中拖入一個button,雙擊進入對應的.cs檔案的編輯,在頭部加入如下的引用:
using System.Windows.Forms;
然後編輯對應的ButtonClick事件
protected void Button1_Click(object sender, EventArgs e)
{
MessageBox.Show("彈出MessageBox對話方塊");
}
3.使用ScriptManager類向用戶端註冊指令碼
ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "aa", "alert('測試')", true);
4.定義一個父類進行調用
可以寫一個父類,裡面定義個messagebox方法,然後讓page繼承父類,這樣子類就可以直接使用父類的方法了,父類中方法如下:
public void MessageBox(UpdatePanel Panel, string Message)
{
string msg = "alert('" + Message + "')";
System.Web.UI.ScriptManager.RegisterStartupScript(Panel, typeof(System.Web.UI.UpdatePanel), "UpdatePanelMessageBox", msg, true);
}
這樣,子類調用MessageBox方法,傳入updatepanel和msg,即可實現彈出