2010-08-02 79 views
2

使用C#消息框在C#

如何顯示在C#中的消息框

我找不到在下拉列表的消息框....

如何顯示網頁中的一個消息框...

+0

你是什麼意思「我找不到下拉列表中的消息框....」? – Yakimych 2010-08-02 15:59:51

+0

您的意思是一個JavaScript警報? – 2010-08-02 16:00:35

回答

2

簡單地說,ASP.NET中沒有MessageBox類。由於ASP.NET在服務器上執行,它將顯示在服務器上而不是客戶機(如果有的話)。你最好的選擇是使用JavaScript或寫你自己的。

下面是創建自己的MessageBox類的ASP.NET

public static class MessageBox 
{ 
    //StringBuilder to hold our client-side script 
    private static StringBuilder builder; 

    public static void Show(string message) 
    { 
     //initialize our StringBuilder 
     builder = new StringBuilder(); 

     //format script by replacing characters with JavaScript compliant characters 
     message = message.Replace("\n", "\\n"); 
     message = message.Replace("\"", "'"); 

     //create our client-side script 
     builder.Append("<script language=\""); 
     builder.Append("javascript\""); 
     builder.Append("type=\"text/javascript\">"); 
     builder.AppendFormat("\t\t"); 
     builder.Append("alert(\"" + message + "\");"); 
     builder.Append(@"</script>"); 

     //retrieve calling page 
     Page page = HttpContext.Current.Handler as Page; 

     //add client-side script to end of current response 
     page.Unload += new EventHandler(page_Unload); 
    } 

    private static void page_Unload(object sender, EventArgs e) 
    { 
     //write our script to the page at the end of the current response 
     HttpContext.Current.Response.Write(builder); 
    } 
} 
1

this讀或只是谷歌,並有數以百計的例子

3

在WinForms的使用MessageBox.Show("Your message");

1

在網頁的一個例子,兩種方式來顯示一個消息是使用JavaScript alert呼叫或AJAX(即ASP.NET AJAX控制工具包)ModalPopupExtender。前者通常更簡單,更容易,但你不會有太多的控制權或能夠支持適當的交互性。