Thursday, February 24, 2011

ASP.NET: FindControl() returns null for an existing control while this control is still accessible via member variable

Building an AJAX enabled ASP.NET application we encountered the effect mentioned in the title of this article.

There is a TabContainer with a TabPanel containing a TextBox.

<cc1:TabPanel runat="server" ID="TabPanel1" TabIndex="0" HeaderText="Person">
<
ContentTemplate>
<asp:TextBox ID="TextBoxName" runat="server"></asp:TextBox>
</
ContentTemplate>
</
cc1:TabPanel>


This TextBox is accessible via a member variable, which is automatically created by Visual Studio Designer:


this.TextBoxName.Text = "Enter name here...";


If you try to access the TextBox using FindControl() method of Page class, you will be alerted with following message:



image



The error message is: “Object reference not set to an instance of an object.”.



That means, the FindControl() method cannot find the control by specified ID an returns null.



Slightly surprised (the TextBox is still accessible via member variable) we looked towards Microsoft Knowledge Base and Forums and found following article: http://msdn.microsoft.com/en-us/library/y81z8326%28v=vs.90%29.aspx#Y2166



It says: “When a control is not inside a naming container, you can get a reference to it by using the control's ID. When a control is inside a naming container, you must call a method that searches the naming container for the control's ID. A control might also be inside a naming container that you do not have direct access to.



To access a control by its ID inside a naming container, you have to extend your Page by instance method FindControlRecursive() described in the referenced article as well:

… 
((TextBox)this.FindControlRecursive(this,"TextBoxName")).Text = "Enter name here..."
… 
private Control FindControlRecursive(Control rootControl, string controlID) 

if (rootControl.ID == controlID) return rootControl; 
    foreach (Control controlToSearch in rootControl.Controls) 
    {


    Control controlToReturn =


                FindControlRecursive(controlToSearch, controlID);


     if (controlToReturn != null) return controlToReturn;


    }


    return null
}



It works! Enjoy…

Wednesday, February 23, 2011

HOWTO: find descendant XElement in XDocument by its name recursively using LINQ with namespaces

If you want to use LINQ to locate XML nodes by their names and your XML contains namespace notations, be aware to specify namespaces looking for descendants.

XML Example:

<?xml version="1.0" encoding="utf-8"?>
<
MyDataStruct xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <
DataRoot xmlns="http://mydata.org/2011/1">
    <
Dispatch xmlns="http://mydata.org/2011/1">
      <
Target>
        <
Organisation>
          <
Comment></Comment>
          <
ID></ID>
        </
Organisation>
        <
System>
          <
ID>100</ID>
          <
Comment></Comment>
        </
System>
      </
Target>
      <
Source>
        <
Organisation>
          <
Comment></Comment>
          <
ID></ID>
        </
Organisation>
        <
System>
          <
ID>101</ID>
          <
Comment></Comment>
        </
System>
      </
Source>
    </
Dispatch>
  </
DataRoot>
</
MyDataStruct>

LINQ based recursive search operations:

XDocument xDoc = XDocument.Load(xmlFileName);

XElement xe1 = xDoc.Descendants("{http://mydata.org/2011/1}Target").First(); // locate first “Target” node
   XElement xe1 = xDoc.Descendants("{http://mydata.org/2011/1}Source").First()
                                  .Descendants("{http://mydata.org/2011/1}ID").First(); // locate “ID” subnode of“Source” node

UPDATE: seen also here -

http://stackoverflow.com/questions/566167/query-an-xdocument-for-elements-by-name-at-any-depth

Enjoy!

Friday, February 11, 2011

Internet Explorer 9 RC to download

Internet Explorer 9 Release Candidate can be downloaded at http://www.beautyOfTheWeb.com or at http://windows.microsoft.com/en-US/internet-explorer/downloads/ie.

OS supported are: Windows Vista/Windows 7/Windows Server 2008/Windows Server 2008 R2. There are already many languages available.

Finally the old known display mode for XML is back! (see my article: Internet Explorer 9 Beta does not display XML files in tree view ).

image

The site http://www.beautyOfTheWeb.com offers interesting information about new features and development tools for IE9.

Additionally there are advertised some featured sites tuned/created for IE9. One of my famous sites is BeatKeep: http://www.beatkeep.net/ - online music editor for (hobby) musicians.

Just download IE9 RC, navigate to BeatKeep and enjoy!