2013-11-10 37 views
0

我已經在asp.net中創建了一個連接類來保存列表中的值我寫了下面的代碼,但它產生的錯誤是MessageBox不存在。我google一下,發現 幾個答案,但這些也不能在這裏工作我的代碼是:在上下文中不存在Asp.net MessageBox

public static class Connection 
    { 
     public static ArrayList GetCoffeeByType(string coffeetype) 
     { 
      ArrayList list = new ArrayList(); 
      try 
      { 
       mydbEntities db = new mydbEntities(); 
       var query = from p in db.tableabc select p; 
       list = query.ToList(); 

      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(exceptionObj.Message.ToString()); 

      } 
      return list; 
     } 
} 

我想這System.Web.UI.ScriptManager.RegisterStartupScript(this, typeof(Page), "alert", "alert('" + Message + "')", true); - 但它也顯示錯誤的thisMessage - 我怎樣才能讓這個MessageBox的?

+0

消息框上的WinForms可用的東西,你應該使用JavaScript的alert()來顯示一個錯誤信息給客戶端,請嘗試使用一個回調 –

+0

你不在Web上不必使用messagebox,而是使用javascript alert來代替 –

回答

1

這是一個叫做Alert的靜態類,帶有一個名爲Show的公共方法。實現儘可能簡單。只需將.cs文件放入網站的App_Code文件夾中即可從所有頁面和用戶控件立即訪問該方法。

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

/// <summary> 
/// A JavaScript alert 
/// </summary> 
publicstaticclass Alert 
{ 

/// <summary> 
/// Shows a client-side JavaScript alert in the browser. 
/// </summary> 
/// <param name="message">The message to appear in the alert.</param> 
publicstaticvoid Show(string message) 
{ 
    // Cleans the message to allow single quotation marks 
    string cleanMessage = message.Replace("'", "\\'"); 
    string script ="<script type=\"text/javascript\">alert('"+ cleanMessage +"');</script>"; 

    // Gets the executing web page 
    Page page = HttpContext.Current.CurrentHandler as Page; 

    // Checks if the handler is a Page and that the script isn't allready on the Page 
    if (page !=null && !page.ClientScript.IsClientScriptBlockRegistered("alert")) 
    { 
     page.ClientScript.RegisterClientScriptBlock(typeof(Alert), "alert", script); 
    } 
}  
} 

示範

那類,您可以在任何時候一個JavaScript警告添加到任何網頁。以下是使用該方法顯示狀態消息的Button.Click事件處理程序的示例。

void btnSave_Click(object sender, EventArgs e) 
{ 
    try 
    { 

     Alert.Show("Your Message"); 
    } 
    catch (Exeception ex) 
    { 
     Alert.Show(ex.Message); 
    } 
} 

你的情況:

public static class Connection 
    { 
     public static ArrayList GetCoffeeByType(string coffeetype) 
     { 
      ArrayList list = new ArrayList(); 
      try 
      { 
       mydbEntities db = new mydbEntities(); 
       var query = from p in db.tableabc select p; 
       list = query.ToList(); 

      } 
      catch (Exception ex) 
      { 
       Alert.Show(ex.Message); 

      } 
      return list; 
     } 
} 
+0

嗨,對不起,遲到的回覆,有一些問題!我可以把App_Code以外的類嗎? – user2835256

+0

是的,你可以像使用其他類一樣使用這個類 –

3

您將無法在ASP.NET應用程序中調用MessageBox.Show。試試這個:

catch (Exception ex) 
{ 
    var script = "alert(" + System.Web.HttpUtility.JavaScriptStringEncode(ex.Message, true) + ")"; 
    System.Web.UI.ScriptManager.RegisterStartupScript(this, typeof(Page), "alert", script, true); 
} 

請注意,您需要獲得捕捉到的異常,exMessage屬性,你應該使用JavaScriptStringEncode安全逃生警報消息。

+0

對不起,對於遲到的回覆,有一些問題!好,所以我試圖用不成功的結果錯誤'System.Web.UI.ScriptManager.RegisterStartupScript(System.Web.UI.Control,System.Type,string,string,bool)'的最佳重載方法匹配有一些無效論點 – user2835256

相關問題