在 gridview 编辑模板内填充下拉列表的函数

发布于 2024-09-26 14:12:11 字数 1110 浏览 10 评论 0原文

我试图为不同的用户角色提供不同的选项。这是我的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

许你一世情深 2024-10-03 14:12:11

小心这一点:绑定(网格/列表/重复器)时使用 <%# %> 而不是 <%= %>

这是 @ 的示例阿德里亚诺斯 说:

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)

careful with this: when binding (grid/list/repeater) use <%# %> and not <%= %>

here's an example of what @adrianos says:

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 my eyes T_T)

断爱 2024-10-03 14:12:11

您可以创建一个在 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.

夜无邪 2024-10-03 14:12:11

我相信您想要的是这样的:

<% 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>

这是我发现在视图的下拉列表中添加动态元素的方法。

I believe that what you want is this:

<% 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>

This is the way that I found out to add dynamic elements in an dropdownlist on a view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文