簡體   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