4

我在 LoginView 的 RoleGroup 中有一些文本框和复选框。如何在我的代码隐藏中访问这些控件?

<asp:LoginView ID="lgvAdmin" runat="server">
        <RoleGroups>
            <asp:RoleGroup Roles="Administrator">
                <ContentTemplate>
                    <div class="floatL">
                        <h1>Administrator Settings</h1>
                        <asp:CheckBox ID="chkActive" Text="Is Active" Checked="false" runat="server" /><br />                    
                        <asp:CheckBox ID="chkIsRep" Text="Is Representative" Checked="false" runat="server" />
                        <br /><br />
                        <strong>User Permissions</strong><br />
                        <asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal" RepeatColumns="3" Width="200" Font-Bold="true">
                            <asp:ListItem Value="User" Selected="True">User</asp:ListItem>
                            <asp:ListItem Value="Administrator">Administrator</asp:ListItem>
                        </asp:RadioButtonList><br /><br />
                    <strong>Assigned to Rep</strong><br />
                    <asp:DropDownList ID="DDLRep" CssClass="ddlStyle" Width="165" runat="server" />
                </div>
            </ContentTemplate>
        </asp:RoleGroup>
    </RoleGroups>
</asp:LoginView>

我知道我需要使用 FindControl 方法,而且我也知道它不仅仅是 lgbvAdmin.FindControl("chkIsRep") 因为控件所在的层次结构。

因此,它应该类似于 lgvAdmin.controls[0].FindControl("chkIsRep");

如何找到访问控件的确切路径?

4

2 回答 2

2

我知道这是一篇旧帖子,但这里有一个关于如何为需要答案的其他人执行此操作的快速示例:

ITemplate template = lgvAdmin.RoleGroups[0].ContentTemplate;
if (template != null)
{
    Control container = new Control();
    template.InstantiateIn(container);

    foreach (Control c in container.Controls)
    {
        if (c is CheckBox)
        {
            //Do work on checkbox
        }
    }
}
于 2011-12-28T21:58:59.957 回答
0

如果请求未通过身份验证,则角色组模板将不适用于页面,并且无法在那里找到使用 if block 如下所示

if(Request.IsAuthenticated)
{
    CheckBox chkactive=(CheckBox)lgvAdmin.FindControl("chkActive");
    chkavtive.Checked=true;
}
于 2018-07-26T14:21:48.317 回答