Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 用于在gridview编辑模板中填充dropdownlist的函数_Asp.net_Templates_Gridview_Drop Down Menu_Codeblocks - Fatal编程技术网

Asp.net 用于在gridview编辑模板中填充dropdownlist的函数

Asp.net 用于在gridview编辑模板中填充dropdownlist的函数,asp.net,templates,gridview,drop-down-menu,codeblocks,Asp.net,Templates,Gridview,Drop Down Menu,Codeblocks,我试图为不同的用户角色提供不同的选项。这是我的密码: 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"

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

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'."

“文本内容(”您可以创建一个在Gridview RowDataBound事件上运行的方法


在该方法中,按id搜索下拉列表。如果找到,请检查用户类型(经理/官员),并以编程方式添加相关的列表项。

您可以创建一个在Gridview RowDataBound事件上运行的方法


在该方法中,按id搜索下拉列表。如果找到它,请检查您的用户类型(经理/官员)并以编程方式添加相关列表项。

注意:绑定(网格/列表/重发器)时使用
,而不是

下面是@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!aaagghh my eyes T_T)

注意:绑定(网格/列表/中继器)时,使用
而不是

下面是@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!aaagghh我的眼睛)

我相信你想要的是:

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

我相信你想要的是:

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

在数据绑定时保留被拒绝的ListItem,在其中声明DropDownList。在数据绑定时保留被拒绝的ListItem,在其中声明DropDownList。