源文件'无法打开未指定的错误.net 4.0

本文关键字:未指定 错误 net 源文件 | 更新日期: 2023-09-27 18:07:30

我是。net框架的新手,正在查看一个简单的联系人表单,当单击按钮时应该提交电子邮件,并且我有问题。

首先,我开始了一个新项目(使用母版页,如果有关系的话),并更改了默认的名称。将SendMail.aspx转换为SendMail.aspx。然后,我将代码复制并粘贴到我创建的项目中。

我在SendMail的标记中得到多个错误和警告,告诉我,例如'Element' Button is not a known element. This can occur if there is a compilation error in the web site for the web.config file is missing。网络。配置文件没有丢失。但是,对于页面

上的每个asp控件,我都会得到一个警告。

我也得到一个编译器错误,告诉我源文件无法打开。然后编译器会列出文件名为Default.aspx.cs的文件路径。这是我在Visual Studio中更改的文件名,但编译器仍在尝试读取该文件名。

这里发生了什么,以便我将来可以解决这些问题?

标记:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="SendMail.aspx.cs" Inherits="SmtpExample._Default" %>
<asp:Content runat="server" ContentPlaceHolderID="HeadContent">
<asp:Panel ID="Panel1" runat="server" DefaultButton="btnSubmit">
    <p>
        Please Fill the Following to Send Mail.</p>
    <p>
        Your name:
        <asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" ErrorMessage="*"
            ControlToValidate="YourName" ValidationGroup="save" /><br />
        <asp:TextBox ID="YourName" runat="server" Width="250px" /><br />
        Your email address:
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*"
            ControlToValidate="YourEmail" ValidationGroup="save" /><br />
        <asp:TextBox ID="YourEmail" runat="server" Width="250px" />
        <asp:RegularExpressionValidator runat="server" ID="RegularExpressionValidator23"
            SetFocusOnError="true" Text="Example: username@gmail.com" ControlToValidate="YourEmail"
            ValidationExpression="'w+([-+.']'w+)*@'w+([-.]'w+)*'.'w+([-.]'w+)*" Display="Dynamic"
            ValidationGroup="save" /><br />
        Subject:
        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="*"
            ControlToValidate="YourSubject" ValidationGroup="save" /><br />
        <asp:TextBox ID="YourSubject" runat="server" Width="400px" /><br />
        Your Question:
        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="*"
            ControlToValidate="Comments" ValidationGroup="save" /><br />
        <asp:TextBox ID="Comments" runat="server" 
                TextMode="MultiLine" Rows="10" Width="400px" />
    </p>
    <p>
        <asp:Button ID="btnSubmit" runat="server" Text="Send" 
                    OnClick="Button1_Click" ValidationGroup="save" />
    </p>
</asp:Panel>
<p>
    <asp:Label ID="DisplayMessage" runat="server" Visible="false" />
</p>  
</asp:Content>
<asp:Content ID="Content1" runat="server" contentplaceholderid="MainContent">
</asp:Content>

背后的代码:

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

namespace SmtpExample
{
    public partial class _SendMail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void SendMail()
        {
            // Gmail Address from where you send the mail
            var fromAddress = "Gmail@gmail.com";
            // any address where the email will be sending
            var toAddress = YourEmail.Text.ToString();
            //Password of your gmail address
            const string fromPassword = "Password";
            // Passing the values and make a email formate to display
            string subject = YourSubject.Text.ToString();
            string body = "From: " + YourName.Text + "'n";
            body += "Email: " + YourEmail.Text + "'n";
            body += "Subject: " + YourSubject.Text + "'n";
            body += "Question: 'n" + Comments.Text + "'n";
            // smtp settings
            var smtp = new System.Net.Mail.SmtpClient();
            {
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                smtp.EnableSsl = true;
                smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
                smtp.Timeout = 20000;
            }
            // Passing values to smtp object
            smtp.Send(fromAddress, toAddress, subject, body);
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                //here on button click what will done 
                SendMail();
                DisplayMessage.Text = "Your Comments after sending the mail";
                DisplayMessage.Visible = true;
                YourSubject.Text = "";
                YourEmail.Text = "";
                YourName.Text = "";
                Comments.Text = "";
            }
            catch (Exception) { }
        }
    }
}

源文件'无法打开未指定的错误.net 4.0

如果您要将元素放置在ContentPlaceHolderID="HeadContent"中,则必须在ContentPlaceHolderID=" MainContent"中

更新

我看到你重命名了默认值。aspx文件发送SendMail。从部分类default.aspx.cs和default.aspx.design.cs中会出现错误。这样做,创建新的页面SendMail。并复制你的代码到ContentPlaceHolderID="HeadContent",右键单击第1页解决方案资源管理器点击设置为起始页