0

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;
}
TJM
  • 89
  • 1
  • 10

1 Answers1

0

After further research (that is, lots of Googling), it appears that I may be running into this problem because I am using a master page. Neither (TextBox)FindControl nor (TextBox)Page.FindControl was working so I have abandoned this approach. Here is an old article that seems to explain my problem.

http://weblog.west-wind.com/posts/2006/Apr/09/ASPNET-20-MasterPages-and-FindControl

TJM
  • 89
  • 1
  • 10