Reputation: 2972
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"));
}
}
}
Upvotes: 0
Views: 12767
Reputation: 21314
Try typing in the 'this' keyword and then searching for your control name in the class behind. However you can not just say agentName.Text. You must either assign it a value or extract the value out into a variable. If it is truly named 'agentName' then you should be able to resolve and build the following:
//Assign the textbox a value
this.agentName.Text = "John";
//Get value out of textbox value
string MyName = this.AgentName.Text;
In addition taks a look to the following line which you state is in the markup behind the page:
// Getting the value from XML
Agentname.Text = root.SelectSingleNode("name").ChildNodes[0].Value;
Ok, you can not place this code in the markup of the page without identifying that it is server code with some 'script' tags identifying it is C# server side code like displayed below:
<script language="C#" runat=server>
// Getting the value from XML
Agentname.Text = root.SelectSingleNode("name").ChildNodes[0].Value;
</script>
Honestly though you really wouldn't even want to place this code in the markup. It should probably be in the Page_Load() event or in another appropriate event after you have populated the XML source that you are trying to extract data from into the Agentname textbox.
Upvotes: 6
Reputation: 17808
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.
Upvotes: 5