Child Controls Showing as Null
-
Hi All, I am currently fighting with a problem with regards to finding controls within ContentPlaceHolders. The scenario is that I want to be able to set the class on a control with a specified ID, which is on one of the Master pages which the page inherits from. There is no limit to how many levels up the tree the control can be, and the code searches recursively until the root master page. This part I have working. :) The issue that I am having is that I can only find a control if it is on the root master page.(I.e. not within a ContentPlaceHolder). If the control is on any other page then FindControl() does not find the control. In this example, the control would only be found if on DefaultMaster. DefaultMaster --> SectionMaster --> PageMaster --> Page.aspx I have investigated further, and found that controls within a Content control are being retured as null, although they clearly exist. Obviously as these are null, FindControl can't find them. I have tried attempting to find a control within the content section also, but I have the same issue. Unfortunately, if you take the above example, then my control would be in SectionMaster, as I dont have access to the root master page. (I'm using Umbraco currently for this project.) Does anyone have any suggestions to how to get these values from content controls? Thanks in advance, DM.
-
Hi All, I am currently fighting with a problem with regards to finding controls within ContentPlaceHolders. The scenario is that I want to be able to set the class on a control with a specified ID, which is on one of the Master pages which the page inherits from. There is no limit to how many levels up the tree the control can be, and the code searches recursively until the root master page. This part I have working. :) The issue that I am having is that I can only find a control if it is on the root master page.(I.e. not within a ContentPlaceHolder). If the control is on any other page then FindControl() does not find the control. In this example, the control would only be found if on DefaultMaster. DefaultMaster --> SectionMaster --> PageMaster --> Page.aspx I have investigated further, and found that controls within a Content control are being retured as null, although they clearly exist. Obviously as these are null, FindControl can't find them. I have tried attempting to find a control within the content section also, but I have the same issue. Unfortunately, if you take the above example, then my control would be in SectionMaster, as I dont have access to the root master page. (I'm using Umbraco currently for this project.) Does anyone have any suggestions to how to get these values from content controls? Thanks in advance, DM.
What event in the Page Lifecycle is your code running? Child controls generally will not be available until after the load event fires for said control. That said, code is not supposed to access controls up the hierarchy only below it. If you need to access something from your parent container, the parent should provide a method to perform said action. Otherwise you introduce unmanageable dependencies.
Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost
-
What event in the Page Lifecycle is your code running? Child controls generally will not be available until after the load event fires for said control. That said, code is not supposed to access controls up the hierarchy only below it. If you need to access something from your parent container, the parent should provide a method to perform said action. Otherwise you introduce unmanageable dependencies.
Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost
I'm using the Page_Load event on the User Control. What I do at first is a FindControl() on the current control, if its not found, it does a a FindControl() on the page which the control is on. If thats not found, it looks at the Master for that Page and continues to do this until the control is found, or it is the root masterpage. It only seems to find controls outwith the content place hold on the root masterpage or nested content controls. Interestingly, on the Controls collection, it shows the correct number of the page, however some are shown as null. I'm sure the nulls are where my controls I need to find are. Thanks DM