簡體   English   中英

C#中的消息框

[英]Message Box in C#

使用C#

如何在C#中顯示消息框

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

如何在網頁中顯示消息框...

在winforms中使用MessageBox.Show("Your message");

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

這是為ASP.NET創建自己的MessageBox類的示例

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);
    }
}

閱讀本文或只是google它,有數百個例子

在網頁中,顯示消息框的兩種方法是使用javascript alert調用或AJAX(即ASP.NET AJAX控件工具包) ModalPopupExtender 前者通常更簡單,更容易,但您無法控制它或能夠支持正確的交互性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM