0

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". I am new in C# programming needs help. 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.";
            }
        }

    }
}
4

3 回答 3

2

当 Code behind 属性中的 .cs 文件名不匹配时,只会出现这种错误。

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

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

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

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

于 2013-06-01T08:18:23.930 回答
0

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

于 2013-06-01T07:51:19.330 回答
0

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

于 2016-09-01T06:26:12.600 回答