I have a .aspx page with several textboxes, including textboxes with IDs of txtID1, txtID2, txtID3... and so on.
I am attempting populate the textboxes with a data from an XML file by looping through a node list. With each loop, I want to use the FindControl
method to locate txtID1 and set its .Text
to the value of the id
attribute of the first node; then locate txtID2 and its .Text to the value of the id
attribute of the second node, and so on.
When the following line of code is run, I get a null reference error for TextBox txtID, so it appears that I am doing something wrong with the FindControl
method. Is my syntax incorrect? Do I need to use a different method?
int x = 1;
XmlNodeList getAuthors = getItem.SelectNodes("item/authors");
foreach (XmlNode getAuthor in getAuthors)
{
TextBox txtID = (TextBox)Page.FindControl("txtID" + x.ToString());
txtID.Text = getAuthor.Attributes["id"].Value.ToString();
x = x + 1;
}