我正在尝试为不同的用户角色提供不同的选项。这是我的代码:
Private Function GetApprovedBy() As String
If User.Identity.Name = "officer" Then
Return "Approved by Officer"
ElseIf User.Identity.Name = "manager" Then
Return "Approved by Manager"
Else
Return String.Empty
End If
End Function
然后在我的gridview模板中,我有:
<EditItemTemplate>
<asp:DropDownList ID="ApprovalEdit" runat="server">
<asp:ListItem>Rejected</asp:ListItem>
<asp:ListItem Text=<%= GetApprovedBy() %>></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
当我运行页面时,我得到了
"Literal content ('<asp:ListItem Text=') is not allowed within a 'System.Web.UI.WebControls.ListItemCollection'."
有没有其他方法可以实现这一目标?最好没有DB。
提前致谢!!
编辑:我也试过
&LT; asp:ListItem><%= GetApprovedBy() %></asp:ListItem>
失败并显示错误'此上下文不支持代码块'
答案 0 :(得分:3)
小心这一点:绑定时(网格/列表/转发器)使用<%# %>
而不是<%= %>
这是@adrianos所说的一个例子:
Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim ddl As DropDownList = CType(e.Row.FindControl("ApprovalEdit"), DropDownList)
' and then do the binding or add some items
End If
End Sub
(vb!aaagghhh,我的眼睛T_T)
答案 1 :(得分:2)
您可以创建一个在Gridview RowDataBound事件上运行的方法。
在该方法中,按ID搜索下拉列表。如果找到它,请检查您的用户类型(经理/官员)并以编程方式添加相关的listItem。
答案 2 :(得分:0)
我相信你想要的是这个:
<% ddlRooms.Items.Clear();
for (int i = 1; i <= 3; i++)
{
ddlRooms.Items.Add(new ListItem(i.ToString() , i.ToString()));
}
%>
<asp:DropDownList ID="ddlRoomsCountToBook" runat="server">
</asp:DropDownList>
这是我发现在视图的下拉列表中添加动态元素的方式。