的FindControl在RoleGroup在LoginView [英] FindControl in a RoleGroup in a LoginView
问题描述
我似乎无法找到一个登录视图控件。
I can't seem to find a control in a login view.
在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>
而code后面的是:
And the code behind is:
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;
}
}
在右角色的用户,我可以看到的复选框,但code后面不填充其中,的FindControl的结果为空。
With a user in the right role, I can see the checkboxes, but the code behind doesn't populate them, the result of the findcontrol is null.
我在想什么?谢谢你。
修改:看起来像我的问题是,当我做了 .FindControl
一个LoginView没有渲染到屏幕上,并返回null 。把我的code上的一个按钮,把它的页面呈现给它的工作,我希望在屏幕后。
Edit: Looks like my issue was when I am doing the .FindControl
the loginview hasn't rendered to the screen and is returning null. Putting my code on a button and calling it after the page has rendered to the screen it works as I would expect.
编辑2 :好像把code最好的地方是 SuperUserLV_ViewChanged
Edit 2: Seems the best place to put the code was SuperUserLV_ViewChanged
推荐答案
内置FindControl方法只搜索直接的子控件。你需要编写方法的递归版本来搜索所有后代。以下是可能需要一些优化一个未经考验的例子:
The built-in FindControl method only searches the direct child controls. You'll need to write a recursive version of the method to search all descendants. Following is an untested example that likely needs some optimization:
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;
}
这篇关于的FindControl在RoleGroup在LoginView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!