【发布时间】:2013-07-18 17:30:32
【问题描述】:
我有一个下拉菜单
<div>
<asp:DropDownList ID="RegistrationDropDownList" runat="server">
<asp:ListItem Value="NULL">All records</asp:ListItem>
<asp:ListItem Value="1">Submitted records</asp:ListItem>
<asp:ListItem Value="0">Non-Submitted records</asp:ListItem>
</asp:DropDownList>
</div>
我想根据会话变量显示/隐藏<asp:ListItem Value="NULL">All records</asp:ListItem>
所以我就这样尝试了
<asp:DropDownList ID="RegistrationDropDownList" runat="server">
<%if (Convert.ToInt32(Session["user_level"]) == 1){ %>
<asp:ListItem Value="NULL">All records</asp:ListItem>
<%}%>
<asp:ListItem Value="1">Submitted records</asp:ListItem>
<asp:ListItem Value="0">Non-Submitted records</asp:ListItem>
</asp:DropDownList>
但是我遇到了一个错误
此上下文不支持代码块
我知道我不能在具有 runat="server" 的控件上使用代码块,但删除它会破坏我的逻辑代码。
我该如何解决这个问题?
【问题讨论】:
-
你能告诉我们逻辑背后的代码吗?
-
听起来条件应该在代码隐藏而不是控制标记中。也许在
Page_Load中,您可以检查条件并从DropDownList中删除NULL值项。 (或相反,检查相反的条件以添加NULL值。) -
你为什么不在后面的代码中构建列表?
-
当您将 runat='server' 添加到 HTML 控件时,您会更改渲染,并且内部不支持代码块。
标签: asp.net drop-down-menu webforms