我有一个具有多个角色组的登录视图,但我无法在代码隐藏中获得转发器参考。我正在使用FindControl并在其上写出中继器的确切ID,但它始终为null ...我需要为每个角色显示一个不同的表,因此我认为loginview是一个不错的选择,而不是在其上发送可见的true false代码隐藏
private void BindListaArtigos()
{
try
{
int userID = Convert.ToInt32(HttpContext.Current.User.Identity.Name);
if (User.IsInRole("Admin"))
{
Repeater rptListaArtigosAdmin = (Repeater)LoginViewRoles.FindControl("rptListaArtigosAdmin");
rptListaArtigosAdmin.DataSource = Requisicao.GetRequisicoes(userID, "Admin");
rptListaArtigosAdmin.DataBind();
}
else if (User.IsInRole("Logistica"))
{
Repeater rptListaArtigosLogistica = (Repeater)LoginViewRoles.FindControl("rptListaArtigosAdmin");
rptListaArtigosLogistica.DataSource = Requisicao.GetRequisicoes(userID, "Logistica");
rptListaArtigosLogistica.DataBind();
}
else if (User.IsInRole("User"))
{
Repeater rptListaArtigosUser = (Repeater)LoginViewRoles.FindControl("rptListaArtigosUser");
rptListaArtigosUser.DataSource = Requisicao.GetRequisicoes(userID, "User");
rptListaArtigosUser.DataBind();
}
}
catch (Exception ex)
{
Response.Write(@"<script language='javascript'>alert('Falha na ligação à base de dados. Tente Novamente mais tarde.');</script>");
Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
}
}
这是具有loginview的页面
<asp:LoginView ID="LoginViewRoles" runat="server">
<RoleGroups>
<asp:RoleGroup Roles="Dev">
<ContentTemplate>
<asp:Repeater ID="rptListaArtigosAdmin" runat="server" OnItemDataBound="rptListaArtigosAdmin_ItemDataBound" OnItemCommand="rptListaArtigosAdmin_ItemCommand">
<HeaderTemplate>
<table id="tblAdmin" class="table table-bordered table-hover">
<thead>
<tr>
<th>ID</th>
<th>Nome</th>
<th>Ref. Interna</th>
<th>Validado Logistica</th>
<th>Estado</th>
<th>Data Criação</th>
<th></th>
<th></th>
</tr>
</thead>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label Font-Bold="true" ID="lblIdReq" runat="server" Text='<%# Eval("IdArtigo") %>' />
<asp:Label Font-Bold="true" ID="lblIdUser" runat="server" Text='<%# Eval("IdUser") %>' />
</td>
<td>
<asp:LinkButton Font-Bold="true" runat="server" ID="lkbtnDetalhes" Text='<%# Eval("Nome") %>' CommandName="Ver" CommandArgument='<%# Eval("IdArtigo") %>' />
</td>
<td>
<asp:Label Font-Bold="true" runat="server" ID="lblRefInterna" Text='<%# Eval("ReferenciaInterna") %>' />
</td>
<td>
<asp:Label Font-Bold="true" ID="lblInfoLog" runat="server" Text='<%# Eval("AprovadoLogistica") %>' />
</td>
<td>
<asp:Label Font-Bold="true" ID="lblEstado" runat="server" Text='<%# Eval("EstadoArtigo") %>' />
</td>
<td>
<asp:Label Font-Bold="true" ID="lblDataCriacao" runat="server" Text='<%# Eval("DataCriacao") %>' />
</td>
<td>
<asp:LinkButton ID="lknCopiar" Font-Bold="true" runat="server" Text="Copiar" CommandName="Copiar" CommandArgument='<%# Eval("IdArtigo") %>' Visible="false" />
</td>
<td>
<asp:LinkButton ID="lknEditar" Font-Bold="true" runat="server" Text="Editar" CommandName="Editar" CommandArgument='<%# Eval("IdArtigo") %>' />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:RoleGroup>
<asp:RoleGroup Roles="Logistica">
<ContentTemplate>
</ContentTemplate>
</asp:RoleGroup>
<asp:RoleGroup Roles="User">
<ContentTemplate>
</ContentTemplate>
</asp:RoleGroup>
</RoleGroups>
</asp:LoginView>