2012-07-10 72 views
1

我在編譯時不斷收到以下錯誤消息:「名稱'消息'在當前上下文中不存在」。我想要做的就是彈出一個消息框。下面是在我的代碼的首頁背後宣佈我的名字空間:當前上下文中不存在名稱'消息'

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Net.Mail; 

,這裏是我的代碼顯示在點擊一個按鈕的消息:

protected void EmailAbout_Click(object sender, EventArgs e) 
    { 
     MailMessage myMessage = new MailMessage(); 
     myMessage.Subject = "Exception Handling Test"; 
     myMessage.Body = "Test message body"; 
     myMessage.From = new MailAddress("[email protected]"); 
     myMessage.To.Add(new MailAddress("[email protected]")); 

     SmtpClient mySmtpClient = new SmtpClient(); 
     mySmtpClient.Send(myMessage); 
     Message.Text = "Message sent"; 
    } 

這是一個缺少命名空間的情況下, ?這似乎很基礎,但我不明白?提前致謝!

這裏是標記頁:

<%@ Page Title="About SalesPro" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" 
CodeBehind="About.aspx.cs" Inherits="WebApplication2.About" %> 

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"> 
</asp:Content> 

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> 
    <h2> 
     About, Inc. 
    </h2> 

    <p> 
     Some Stuff here... 
    </p> 

    <p> 
     <asp:Button ID="EmailAbout" runat="server" Text="Email Information" 
      onclick="EmailAbout_Click" /> 
    </p> 
</asp:Content> 
+0

這是一個web應用程序? – 2012-07-10 16:14:48

+2

什麼是'消息'應該是? – 2012-07-10 16:15:57

+0

你的頁面上有一個名爲'Message'的控件嗎? – vcsjones 2012-07-10 16:16:08

回答

0

您需要javaScript-function「alert()」:-)

試試這個:

RegisterStartupScript("alertBox1", "<script type='text/javascript'>alert('Message sent');</script>"); 
+0

這工作,並可能是最好的多客戶端的方式,但我想知道我沒有使用Javascript沒有這樣做嗎? – 2012-07-10 17:24:25

+0

JavaScript是在瀏覽器中彈出交互框或其他東西的唯一方式。或者,您可以使用標籤或文本框來發送此結果,這些工作不需JavaScript。 – MaxKlaxxMiner 2012-07-10 17:38:28

+0

將答案放在klicked按鈕中的示例:EmailAbout.Text =「發送的郵件」; – MaxKlaxxMiner 2012-07-10 17:41:28

1

我想的標籤信息不可用在運行時,或不存在

更新答:

Response.Write("<script>alert('Message Sent');</script>"); 
+2

你怎麼知道這是一個標籤? – jrummell 2012-07-10 16:20:26

+0

bcz標籤具有文本屬性。並且在發送電子郵件之後用戶有消息「發送消息」。所以我猜想。無需投票。 – 2012-07-10 16:29:15

+0

這不是我之後的標籤,而是在屏幕上顯示一個彈出消息。 – 2012-07-10 17:27:18

1

右鍵單擊參考文獻添加System.Windows.Forms然後替換。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Net.Mail; 

隨着

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Net.Mail; 
using System.Windows.Forms; 

希望這有助於。

0

就我而言,我已經通過將我的名稱空間直接放入需要它的c#(.cs)文件來解決此問題。出於某種原因,將其添加到app_code中無法正常工作,即使我已經能夠使用intellisense拾取名稱空間。

using System; 
using System.Collections.Generic; 
using System.Data; 
using System.Configuration; 
using System.Text; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 

public partial class SmartFutures_Default : System.Web.UI.Page 
{ 
... 
... 
    webMessageBox.Show("THANK YOU. YOUR REGISTRATION HAS BEEN SUBMITTED"); 
... 
} 

/// <summary> 
/// A JavaScript alert class 
/// </summary> 
public static class webMessageBox 
{ 

    /// <summary> 
    /// Shows a client-side JavaScript alert in the browser. 
    /// </summary> 
    /// <param name="message">The message to appear in the alert.</param> 
    public static void Show(string message) 
    { 
     // Cleans the message to allow single quotation marks 
     string cleanMessage = message.Replace("'", "\\'"); 
     string wsScript = "<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")) 
     { 
      //ClientScript.RegisterStartupScript(this.GetType(), "MessageBox", wsScript, true); 
      page.ClientScript.RegisterClientScriptBlock(typeof(webMessageBox), "alert", wsScript, false); 
     } 
    } 
} 
相關問題