C# 3.5:如何从UserControl中获取父类的名称?

3

C# 3.5:如何从UserControl内部动态获取父类的名称?

例如,如果 TestPage.aspx 包含 UserControl ucTestUc,我需要知道其父类是 TestPage。

我尝试了以下代码,在 ucTestUc 内部:

this.Parent.NamingContainer 是 ASP.testpage_aspx。

虽然很接近,但还不够准确。我可以去掉前缀和后缀,但无法恢复大写字母。

请问有什么方法可以解决这个问题吗?

1个回答

6
string typeName = this.Page.GetType().Name;

string baseTypeName = this.Page.GetType().BaseType.Name;

string fileName = Path.GetFileName(Request.PhysicalPath);

或者 if (Page.GetType() is TestPage) { ... } - Nelson Rothermel
1
@Lill:你要求父类,而 this.Page.GetType().BaseType.Name 将以正确的大小写给出父类名称。 - LukeH
最佳答案是this.Page.GetType().BaseType.Name,因为它恰好给了我需要的东西:TestPage。 第二好的答案是Path.GetFileName(Request.PhysicalPath),因为它给了我TestPage.aspx,我可以去掉.aspx。谢谢Luke! - Lill Lansey

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接