Reputation: 1841
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'
Upvotes: 1
Views: 2888
Reputation: 1858
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.
Upvotes: 0
Reputation: 1659
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)
Upvotes: 3
Reputation: 1561
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.
Upvotes: 2