Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从子页访问母版页用户控件_C#_Asp.net - Fatal编程技术网

C# 从子页访问母版页用户控件

C# 从子页访问母版页用户控件,c#,asp.net,C#,Asp.net,我有一个包含usercontrol的母版页。 现在Abc.aspx是母版页的子页。现在,子页还有一个用户控件。 我的要求是从子页面的用户控件中获取母版页的用户控件 母版页aspx 我的要求是隐藏此特定页面的母版页用户控件 更新: 我有办法了谢谢 面临的问题 MuduleListLeftPanel muduleListLeftPanel = this.Master.LeftPanel; UserProfilePic userProfile = this.Master.UserProfile; mu

我有一个包含usercontrol的母版页。 现在Abc.aspx是母版页的子页。现在,子页还有一个用户控件。 我的要求是从子页面的用户控件中获取母版页的用户控件

母版页aspx 我的要求是隐藏此特定页面的母版页用户控件

更新: 我有办法了谢谢

面临的问题

MuduleListLeftPanel muduleListLeftPanel = this.Master.LeftPanel;
UserProfilePic userProfile = this.Master.UserProfile;
muduleListLeftPanel.Visible = false; // hide sucessfully
userProfile.Attributes["style"] = "display:none"; // non working .. I need to use display none.. for both user control

首先,使用母版页上的
FindControl
查找用户控件(ID为
UserControlOnMaster
)。因此,在页面上用户控件的某个地方,使用以下代码

WebUserControl1 control = Page.Master.FindControl("UserControlOnMaster") as WebUserControl1;
找到后,您可以访问该用户控件中的其他控件

Label LabelOnMaster = control.FindControl("Label1") as Label;
LabelOnMaster.Text = "Control found!";

首先,使用母版页上的
FindControl
查找用户控件(ID为
UserControlOnMaster
)。因此,在页面上用户控件的某个地方,使用以下代码

WebUserControl1 control = Page.Master.FindControl("UserControlOnMaster") as WebUserControl1;
找到后,您可以访问该用户控件中的其他控件

Label LabelOnMaster = control.FindControl("Label1") as Label;
LabelOnMaster.Text = "Control found!";

由于用户控件在父控件中受保护,因此需要使用
public
函数来更新控件并从子控件访问它

例如,在母版页中:

公共部分类SiteMaster:System.Web.UI.MasterPage
{
public void SetMyUserControlVisibility(bool可见)
{
MyUserControl.Visible=可见;
}
}
现在只需在子页面中:

公共部分类MyPage:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!IsPostBack)
{
SiteMaster主页=(SiteMaster)this.Page.Master;
//在此处更新母版页的用户控件
masterPage.SetMyUserControlVisibility(true);
}
}
}

由于用户控件在父控件中受
保护
,因此您需要具有
public
功能来更新控件并从子控件访问它

例如,在母版页中:

公共部分类SiteMaster:System.Web.UI.MasterPage
{
public void SetMyUserControlVisibility(bool可见)
{
MyUserControl.Visible=可见;
}
}
现在只需在子页面中:

公共部分类MyPage:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!IsPostBack)
{
SiteMaster主页=(SiteMaster)this.Page.Master;
//在此处更新母版页的用户控件
masterPage.SetMyUserControlVisibility(true);
}
}
}

可能重复的“否”,我想访问母版页的用户控件。请再次阅读我的问题。最终获得解决方案&感谢“否”的后可能重复,我想访问母版页的用户控件。请再次阅读我的问题。最终获得解决方案&感谢“是”,我已经这样做了,但我的代码仍然没有帮助..受保护的无效页面加载(对象发送者,事件参数e){System.Web.UI.UserControl control=Page.Master?.FindControl(“MuduleListLeftPanel”)作为System.Web.UI.UserControl;//如果(control==null)从子页面获取用户控件{抛出新的NullReferenceException(“无效对象”);}您必须搜索正确的用户控件类型,不是
usercontrol
,而是
WebUserControl1
,正如在我的回答中一样,先生,我已经更新了我的代码,但仍然出现相同的错误。我还发布了我正在使用的代码。您的
lbl1
不在用户控件内,而是在主机上(那将是
Page.Master.FindControl(“lbl1”)
)。如果您想隐藏控件,为什么不使用
Control.Visible=false;
谢谢您的回复,先生,实际上,我在母版页上放置了一个标签进行测试,我认为在访问UserControl时可能会出现一些问题,但我错了,标签也出现了同样的问题。我已经更新了我的代码@vdwdsirYes,我已经这样做了,但我的代码仍然没有帮助..受保护的无效页面加载(对象发送者,事件参数e){System.Web.UI.UserControl=Page.Master?.FindControl(“MuduleListLeftPanel”)作为System.Web.UI.UserControl;//如果(control==null)从子页面获取用户控件{抛出新的NullReferenceException(“无效对象”);}您必须搜索正确的用户控件类型,不是
usercontrol
,而是
WebUserControl1
,正如在我的回答中一样,先生,我已经更新了我的代码,但仍然出现相同的错误。我还发布了我正在使用的代码。您的
lbl1
不在用户控件内,而是在主机上(那将是
Page.Master.FindControl(“lbl1”)
)。如果您想隐藏控件,为什么不使用
Control.Visible=false;
谢谢您的回复,先生,实际上,我在母版页上放置了一个标签进行测试,我认为在访问UserControl时可能会出现一些问题,但我错了,标签也出现了同样的问题。我已经更新了我的代码@vdwds红外光谱
MuduleListLeftPanel muduleListLeftPanel = this.Master.LeftPanel;
UserProfilePic userProfile = this.Master.UserProfile;
muduleListLeftPanel.Visible = false; // hide sucessfully
userProfile.Attributes["style"] = "display:none"; // non working .. I need to use display none.. for both user control
WebUserControl1 control = Page.Master.FindControl("UserControlOnMaster") as WebUserControl1;
Label LabelOnMaster = control.FindControl("Label1") as Label;
LabelOnMaster.Text = "Control found!";