繁体   English   中英

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

[英]The name 'txtname' doesn't exist in the current context

我正在使用Visual "The name 'txtname' doest not exists in the current context" 。当我运行asp页面时,它显示错误"The name 'txtname' doest not exists in the current context" 我是C#编程新手,需要帮助。 我正在使用所有定义的变量,但是我仍然很困惑为什么它会产生错误。

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.Net;
namespace WebApplication1
{
    public partial class contactus : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                //Create the msg object to be sent
                MailMessage msg = new MailMessage();
                //Add your email address to the recipients
                msg.To.Add("myinfo@yahoo.com");
                //Configure the address we are sending the mail from
                MailAddress address = new MailAddress("abc@gmail.com");
                msg.From = address;
                //Append their name in the beginning of the subject
                msg.Subject = txtName.Text + " :  " + txtName1.Text;
                msg.Body = txtMessage.Text;

                //Configure an SmtpClient to send the mail.
                SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
                client.EnableSsl = true; //only enable this if your provider requires it
                //Setup credentials to login to our sender email address ("UserName", "Password")
                NetworkCredential credentials = new NetworkCredential("abc@gmail.com", "paswword");
                client.Credentials = credentials;

                //Send the msg
                client.Send(msg);

                //Display some feedback to the user to let them know it was sent
                lblResult.Text = "Your email has been received, you will be contacted soon by our representative if required.";

                //Clear the form
                txtName.Text = "";
                txtName1.Text = "";
                txtMessage.Text = "";
            }
            catch
            {
                //If the message failed at some point, let the user know
                lblResult.Text = "Your message failed to send, please try again.";
            }
        }

    }
}

当“身后代码”中的.cs文件名不匹配时,仅会发生这种错误。

检查@Page directive上文件名和Inherits属性后面的代码,确保它们匹配。

或者似乎您已经复制并粘贴了与之相关的控件或代码。

这通常会在designer文件中产生问题。

您可以删除该文本框,然后再次将其从toolbar拖放到您的应用中

如果它是常规的html控件,则在txtName控件中添加runat="server"

我知道这是老问题了。 在单独的地方复制了几个div后,我遇到了同样的问题。 这对将来的任何人都可能有用。 只需删除relevenat标记的runat =“ server”属性,然后再次插入即可。 它为我工作了100%。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM