views:

53

answers:

2

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'

+2  A: 

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.

adrianos
+3  A: 

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)

y34h
To keep the Rejected ListItem when Data Binding set AppendDataBoundItems="true" where you declared the DropDownList.
Daniel Ballinger
+1 for code sample!
adrianos