在LoginView的RoleGroup中找到控件

发布于 2024-09-28 15:07:08 字数 1721 浏览 0 评论 0原文

我在 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");

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

I have some textboxes and checkboxes inside a RoleGroup of a LoginView. How can I access these controls in my code-behind?

<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>

I know I need to use the FindControl method and I also know it isn't just lgbvAdmin.FindControl("chkIsRep") because of the hierarchy of where the control is.

So, it should be something like, lgvAdmin.controls[0].FindControl("chkIsRep");

How can I find the exact path to access my control?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谜泪 2024-10-05 15:07:08

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

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
        }
    }
}

I know this is an old post but here is a quick sample on how to do this for anyone else who needs the answer:

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
        }
    }
}
可爱暴击 2024-10-05 15:07:08

如果请求未经身份验证,则角色组模板将不适用于页面,并且无法在页面中找到可供使用的 if 块,如下所示

if(Request.IsAuthenticated)
{
    CheckBox chkactive=(CheckBox)lgvAdmin.FindControl("chkActive");
    chkavtive.Checked=true;
}

if request is not authenticated the rolegroup template won't apply to page and can't be found there for use if block like below

if(Request.IsAuthenticated)
{
    CheckBox chkactive=(CheckBox)lgvAdmin.FindControl("chkActive");
    chkavtive.Checked=true;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文