在 gridview 编辑模板内填充下拉列表的函数
我试图为不同的用户角色提供不同的选项。这是我的代码:
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'."
是否有其他方法可以实现此目的?最好没有数据库。
提前致谢!!
编辑:我也尝试过
<asp:ListItem><%= GetApprovedBy() %>
失败,并出现错误“在此上下文中不支持代码块”
I am trying to have different options for different user roles. Here is my code:
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
Then inside my gridview templates I have:
<EditItemTemplate>
<asp:DropDownList ID="ApprovalEdit" runat="server">
<asp:ListItem>Rejected</asp:ListItem>
<asp:ListItem Text=<%= GetApprovedBy() %>></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
When I run the page I get
"Literal content ('<asp:ListItem Text=') is not allowed within a 'System.Web.UI.WebControls.ListItemCollection'."
Is there an alternative way of achieving this? Preferably without a DB.
Thanks in advance!!
Edit: I have also tried
<asp:ListItem><%= GetApprovedBy() %></asp:ListItem>
which failed with error 'Code blocks are not supported in this context'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
小心这一点:绑定(网格/列表/重复器)时使用
<%# %>
而不是<%= %>
这是 @ 的示例阿德里亚诺斯 说:
(vb!aaagghhh 我的眼睛 T_T)
careful with this: when binding (grid/list/repeater) use
<%# %>
and not<%= %>
here's an example of what @adrianos says:
(vb! aaagghhh my eyes T_T)
您可以创建一个在 Gridview RowDataBound 事件上运行的方法。
在该方法中,通过 id 搜索下拉列表。如果找到它,请检查您的用户类型(经理/官员)并以编程方式添加相关的列表项。
You could create a method that runs on the Gridview RowDataBound event.
In that method, search for your drop down list by id. If you find it, check your user type (manager / officer) and add the relevant listItems programmatically.
我相信您想要的是这样的:
这是我发现在视图的下拉列表中添加动态元素的方法。
I believe that what you want is this:
This is the way that I found out to add dynamic elements in an dropdownlist on a view.