我似乎在登录视图中找不到控件。
aspx是:
<asp:LoginView ID="SuperUserLV" runat="server">
<RoleGroups>
<asp:RoleGroup Roles="SuperUser">
<ContentTemplate>
<asp:CheckBox ID="Active" runat="server" /><br />
<asp:CheckBox ID="RequireValidaton" runat="server" />
</ContentTemplate>
</asp:RoleGroup>
</RoleGroups>
</asp:LoginView> 背后的代码是:
if (Context.User.IsInRole("SuperUser"))
{
CheckBox active = (CheckBox) SuperUserLV.FindControl("Active");
if (active != null)
{
active.Checked = this.databaseObject.Active;
}
CheckBox require = (CheckBox) SuperUserLV.FindControl("RequireValidaton");
if (require != null)
{
require.Checked = this.databaseObject.RequiresValidation;
}
}有了正确角色的用户,我可以看到复选框,但是后面的代码没有填充它们,findcontrol的结果是空的。
我遗漏了什么?谢谢。
编辑:看起来我的问题是当我做.FindControl时,loginview没有呈现到屏幕上,而是返回null。将我的代码放在一个按钮上,并在页面呈现到屏幕后调用它,它就像我所期望的那样工作。
编辑2:似乎最好的地方放置代码是SuperUserLV_ViewChanged
发布于 2008-12-16 18:39:04
内置FindControl方法仅搜索直接子控件。您需要编写该方法的递归版本,以搜索所有后代。下面是一个未经测试的示例,可能需要进行一些优化:
public Control RecursiveFindControl(Control parent, string idToFind)
{
for each (Control child in parent.ChildControls)
{
if (child.ID == idToFind)
{
return child;
}
else
{
Control control = RecursiveFindControl(child, idToFind);
if (control != null)
{
return control;
}
}
}
return null;
}https://stackoverflow.com/questions/372158
复制相似问题