简体   繁体   English

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

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

I am using visual studio 2010. When i run the asp page it shows the errors "The name 'txtname' doest not exists 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" I am new in C# programming needs help. 我是C#编程新手,需要帮助。 I am using all the defined variables but i am still confused why it is giving errors. 我正在使用所有定义的变量,但是我仍然很困惑为什么它会产生错误。

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.";
            }
        }

    }
}

When .cs file name in Code behind attribute does not match , then only this kind of error can occur. 当“身后代码”中的.cs文件名不匹配时,仅会发生这种错误。

Check your code behind file name and Inherits property on the @Page directive , make sure they both match. 检查@Page directive上文件名和Inherits属性后面的代码,确保它们匹配。

Or it seems that you have copy and pasted the control or code related to it. 或者似乎您已经复制并粘贴了与之相关的控件或代码。

This often creates problem in designer file. 这通常会在designer文件中产生问题。

You can delete that textbox and once again drag-drop it on your app from toolbar 您可以删除该文本框,然后再次将其从toolbar拖放到您的应用中

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

I know this is and old question. 我知道这是老问题了。 I got the same problem after copying few divs in a seperate place. 在单独的地方复制了几个div后,我遇到了同样的问题。 This might be useful to anyone in future. 这对将来的任何人都可能有用。 Just remove runat="server" attribute of the relevenat tag and again insert it. 只需删除relevenat标记的runat =“ server”属性,然后再次插入即可。 It worked for me 100%. 它为我工作了100%。

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

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