Reputation: 23
We have some code which is designed to build up a key/value pair of controls on a ASP.NET webforms page and their values. We use the "Controls" collection and recursion to build up this key/value pair.
The only issue is that ascx controls don't appear to come back in the "Controls" collection. Instead we get the controls on the ascx.
Something similar to the following code:
protected override void OnLoad(EventArgs e)
{
GetControls(this);
}
protected void GetControls(System.Web.UI.Control ctl)
{
foreach (Control subCtl in ctl.Controls)
{
GetControls(subCtl);
string value;
if (GetValueFromControl(subCtl, out value))
{
_ControlValues.Add(subCtl.ClientID, value);
}
}
}
The subCtl is never a ascx control. Is there a way to get a list of the ascx controls on page or better still a different collection that contains both types?
Thanks
Hans
Upvotes: 2
Views: 190
Reputation: 586
.ascx controls do appear in the controls collection.
Looking at your code, I wonder if this is perhaps an issue with the GetValueFromControl function -- perhaps it's not functioning correctly for .ascx controls, or whatever value you're looking for is not set? Or perhaps the equivalent recursion function in your real code is only adding leaf nodes to _ControlValues, and skipping over nodes with children?
Upvotes: 1
Reputation: 703
As far as I know, ASCX controls are included in the Controls
collection.
I've made a class inheriting off of System.Web.UI.WebPage
that would iterate through all of the controls in this.Controls
and it included all of the ASCX classes.
Upvotes: 1