我在我的主页上有一个用户控件,在角色组下的登录视图下。null结果。如何从背后的页面代码中访问此内容。我有问题从主页获取。
<asp:RoleGroup Roles="Students">
<ContentTemplate>
<uc1:studentsPanel runat="server" ID="studentcontrol" />
</ContentTemplate>
</asp:RoleGroup>
这是我的代码
的方式LoginView control = Page.Master.FindControl("studentcontrol") as LoginView;
if (control != null)
{
Label1.Text = "found";
}
这是我用来进入MasterPages中的控件的代码
//Master page from user control
LoginControl control
Page page = (Page)this.Page;
MasterPage master = (MasterPage)page.Master;
control= (LoginControl )master.FindControl("studentcontrol");
if (control!= null)
{
Label1.Text = "found";
}
我们看不到整个代码,但是您的摘要看起来正确。首先尝试不要将控制权投入到登录时 - 原因可能是您的面板不是该类型。要尝试是否找到控件,请使用
if(Page.Master.FindControl("studentcontrol") != null) {
Label1.Text = "found";
}
首先添加另一个可能的故障来源。
还有两个明确的答案在这里不需要重复。您在这里和这里找到了很棒的解释。
@krishnraj所说,
我不知道StudentPanel UserControl中哪个是控制的,但是我认为该标签。您应该这样访问,
var Loginview = (Master.FindControl("LoginView1") as LoginView);
Control cont = new Control();
Loginview.RoleGroups[0].ContentTemplate.InstantiateIn(cont);
(cont.Controls[1].FindControl("_trylbl") as Label).Text = "Hello say";