The error I get is :The name agentName does not exist in the current context”
On default.aspx I have <asp:TextBox ID="agentName" runat="server" />
On code behind file I have agentName.Text
But is says the above error message.
when I pass hard coded value like “John”, it works. I need a way to recognize the textbox on code-behind.
Thanks
Following is my code:
Default.aspx:
//code behind on @page directive
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="WebApplication2.WebForm1"%>
// Getting the value from XML
Agentname.Text = root.SelectSingleNode("name").ChildNodes[0].Value;
// Assigned a textbox
asp:TextBox ID="Agentname" runat="server" disabled="true" MaxLength="57" /
(removed angel bracket as SO is ignoring this line, don't know why)
//Code behind
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace WebApplication2
{
public partial class WebForm1 : System.Web.UI.Page
{
public void SaveXml(object sender, EventArgs e)
{
XDocument xmlDoc = XDocument.Load(Server.MapPath("Agent.xml"));
Console.WriteLine("read XML");
var person = xmlDoc.Descendants("agent");
person.ElementAt(0).Value = "";
xmlDoc.Element("agent").Add(new XElement("name", Agentname.Text));
xmlDoc.Save(Server.MapPath("Agent.xml"));
}
}
}
Sometimes the designer file gets out of sync with the aspx file. Delete the “YourPage.aspx.designer.cs” file, then right click the form in the solution explorer and select something like “Covert to Web Application”
This will regenerate the designer to match the current aspx.