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("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

4 回答 4

1

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

更新答案:

Response.Write("<script>alert('Message Sent');</script>");
于 2012-07-10T16:18:49.893 回答
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;

希望这可以帮助。

于 2013-11-26T19:11:45.313 回答
0

您需要 javaScript 函数“alert()”:-)

试试这个:

RegisterStartupScript("alertBox1", "<script type='text/javascript'>alert('Message sent');</script>");
于 2012-07-10T16:36:43.443 回答
0

就我而言,我通过将命名空间直接放入需要它的 c# (.cs) 文件中解决了这个问题。出于某种原因,将其添加到 app_code 中不起作用,即使我已经能够使用智能感知获取命名空间。

IE

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);
        }
    }
}
于 2015-05-04T07:14:33.573 回答