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?
在aspx中我有多个用户控件。在其中一个用户控制中,我需要遍历其他用户控件并获取它们的物理路径。有没有简单的方法来做到这一点?
1 个解决方案
#1
3
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);
}
}
#1
3
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);
}
}