1

コンパイル時に次のエラー メッセージが表示され続けます: 「名前 '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

4 に答える 4

1

ラベルメッセージは実行時に利用できないか、存在しないと思います

更新された回答:

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 に追加しても機能しませんでした。

すなわち

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 に答える