Question

I have a problem regarding getting the path of a user control. The scenario is as follows:

In a aspx i have multiple user controls. In one of those user conrtols i need to loop through the other user controls and get the physical path of them. Is there any easy way to do this?

Was it helpful?

Solution

List<string> GetUserControlPathsForPage { 
     var list = new List<string>();
     return getUserControlPathsRecursive(Page.Controls, list);
 } 

void getPathsRecursive(ControlCollection controls, List<string> list) {  
     foreach (var c in controls) {  
        var uc = c as UserControl;
        if (uc != null) { 
            list.Add(Server.MapPath(uc.AppRelativeVirtualPath));
        }
        getPathsRecursive(c.Controls,list);
     }
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top