首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FindControl in a RoleGroup in a LoginView

FindControl in a RoleGroup in a LoginView
EN

Stack Overflow用户
提问于 2008-12-16 18:10:29
回答 1查看 2.9K关注 0票数 0

我似乎在登录视图中找不到控件。

aspx是:

代码语言:javascript
复制
<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> 

背后的代码是:

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2008-12-16 18:39:04

内置FindControl方法仅搜索直接子控件。您需要编写该方法的递归版本,以搜索所有后代。下面是一个未经测试的示例,可能需要进行一些优化:

代码语言:javascript
复制
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;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/372158

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档