I can’t seem to find a control in a login view.
The aspx is:
<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>
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; } }
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.
What am I missing? Thanks.
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.
Edit 2: Seems the best place to put the code was SuperUserLV_ViewChanged
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: