我现在负责一个ASP.net VB Web表单应用程序,我需要对其进行更改。我对asp.net没有多少经验,所以如果我没有正确的流程,请告诉我。我试图隐藏两个表行,具体取决于转发器内嵌套表内的用户权限 - 但TR的ID不会出现在设计器中,如果我手动添加它们,他们就不会这样做。在VS删除之前,请停留很长时间。在后面的代码中,我为这两个ID收到了is not declared
个错误
<table>
<asp:repeater ID="history" runat="server" OnItemDataBound="historyDetails">
<itemTemplate>
<tr>
<td>
<table>
<%--
I'm trying to hide these 2 rows conditionally
without using <% if isAdmin then %> <% end if %>
--%>
<tr id = "adminHeading" runat="server">
<td>...</td>
</tr>
<tr id = "adminNav" runat="server">
<td>...</td>
</tr>
<tr>...</tr>
</table>
</td>
</tr>
</itemTemplate>
</asp:repeater>
</table>
背后的代码
Protected Sub historyDetails(ByVal sender As Object)
...
...
If session("isAdmin") Then
adminHeading.visible = True 'produces not declared error
adminNav.visible = True 'produces not declared error
End If
End Sub
答案 0 :(得分:2)
您无法访问adminHeading
&amp;的原因您的代码中的adminNav
是因为它们不直接存在于您的表单标记中,而是存在于转发器控件中。您可以通过查找这些表行来完成ItemDataBound事件: -
Protected Sub historyDetails(sender As Object, e As RepeaterItemEventArgs)
If (e.Item.ItemType = ListItemType.AlternatingItem Or _
e.Item.ItemType = ListItemType.Item) Then
Dim adminHeading As HtmlTableRow = TryCast(e.Item.FindControl("adminHeading"), _
HtmlTableRow)
Dim adminNavAs HtmlTableRow = TryCast(e.Item.FindControl("adminNav"), _
HtmlTableRow)
'Check your condition and hide the rows
If session("isAdmin") Then
adminHeading.Visible = False
adminNavAs.Visible = False
End If
End If
End Sub