ASP.Net - C# - 传递页为参数 [英] ASP.Net - C# - Passing Page as parameter
问题描述
我有一个网站。从网站的每个页面我要打电话将接收的页面类型的参数的函数。每个页面将通过自身参照该功能。
I have a website. from each page of website I want to call a function which will receive a parameter of type Page. Each page will pass reference of itself to that function.
这功能将隐藏和显示基于某种逻辑页面上一些控制。
That function will hide and show some control on that page based on some logic.
现在我不知道如何通过页面参数。如果我通过这个
,我无法找到我要隐藏或显示任何控件。这是我的功能
Now I am not sure how to pass the page parameter. If I pass "this
", I am unable to find any controls which I want to hide or show. This is my function
public static void Implement(string pageName, Page objPage)
{
if (pageName == "MANAGEMENT")
{
HyperLink obj = (HyperLink) objPage.FindControl("hlSave");
if (obj != null)
{
obj.Visible = false;
}
}
}
但 objPage.FindControl(hlSave);
始终返回null
任何想法什么错在这里?
Any idea whats wrong here?
推荐答案
如果您使用的是母版页
则可能引起的FindControl
返回空
。在这种情况下,你可以使用:
If you are using master page
then that might causing FindControl
to return null
. In that case you can use:
HyperLink obj = (HyperLink)objPage.Master.FindControl("ContentPlaceHolderID").FindControl("hlSave");
也可以递归找到 hlSave
使用下面的方法:
public static Control FindControlRecursive(Control Root, string Id)
{
if (Root.ID == Id)
return Root;
foreach (Control Ctl in Root.Controls)
{
Control FoundCtl = FindControlRecursive(Ctl, Id);
if (FoundCtl != null)
return FoundCtl;
}
return null;
}
您可以用它喜欢的:
HyperLink obj = (HyperLink)FindControlRecursive(objPage, "hlSave");
这篇关于ASP.Net - C# - 传递页为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!