当前上下文中不存在名称“Message”

时间:2012-07-10 16:13:22

标签: c# asp.net

我在编译时不断收到以下错误消息:“当前上下文中不存在名称'Message'”。我要做的就是弹出一个消息框。以下是在我的代码页面顶部声明的命名空间:

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("me@yourprovider.com");
        myMessage.To.Add(new MailAddress("you@yourprovider.com"));

        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>

4 个答案:

答案 0 :(得分:1)

我认为标签Message在运行时不可用或者不存在

更新的答案:

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

答案 1 :(得分: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;

希望这有助于。

答案 2 :(得分:0)

您需要javaScript-function“alert()”: - )

试试这个:

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

答案 3 :(得分: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);
        }
    }
}