0

I have a dropdown

 <div>
     <asp:DropDownList ID="RegistrationDropDownList" runat="server">
         <asp:ListItem Value="NULL">All records</asp:ListItem>
         <asp:ListItem Value="1">Submitted records</asp:ListItem>
         <asp:ListItem Value="0">Non-Submitted records</asp:ListItem>
     </asp:DropDownList>
 </div>

I want to Show/hide <asp:ListItem Value="NULL">All records</asp:ListItem> based on a session variable

So I tried like this

 <asp:DropDownList ID="RegistrationDropDownList" runat="server">
 <%if (Convert.ToInt32(Session["user_level"]) == 1){ %>
     <asp:ListItem Value="NULL">All records</asp:ListItem>
 <%}%>
     <asp:ListItem Value="1">Submitted records</asp:ListItem>
     <asp:ListItem Value="0">Non-Submitted records</asp:ListItem>
 </asp:DropDownList>

But I got an error

code blocks are not supported in this context

I understand I cant use code blocks on controls that have the runat="server" but removing it breaks my code behind logic.

How can I solve this problem ?

4
  • Can you show us the code behind logic? Commented Jul 18, 2013 at 17:33
  • 2
    It sounds like that conditional should be in the code-behind instead of the control mark-up. Maybe in Page_Load you can check the conditional and remove the NULL value item from the DropDownList. (Or the inverse, check the opposite condition to add the NULL value.) Commented Jul 18, 2013 at 17:33
  • 2
    why don't you just build the list in code behind? Commented Jul 18, 2013 at 17:33
  • When you add a runat='server' to an HTML control you change the rendering and code blocks aren't supported inside. Commented Jul 18, 2013 at 17:36

3 Answers 3

3

This should be done in code behind:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack && Convert.ToInt32(Session["user_level"]) == 1)
    {
        RegistrationDropDownList.Items.Insert(0, new ListItem("All records", "NULL"));
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Blah 2 minutes Good job
Thanks that solved my problem, do you know why it defaults to the second listitem (Submitted records) after loadin
0

Add it in the code behind

if (Convert.ToInt32(Session["user_level"]) == 1)
            {
                ListItem newItem = new ListItem();
                newItem.Value = null;
                newItem.Text = "All record";
                DropDownList1.Items.Add(newItem);

            }

Comments

0

It's a shame that ListItem doesn't have a Visibility property that you could bind to. The best solution I can think of is to write a class that extends DropDownList; add a method that inserts the "All Records" item before rendering.

public class ExtendedList : DropDownList
{
    protected override void OnLoad(EventArgs e)
    {
        if (Convert.ToInt32(Session["user_level"]) == 1)
        {
            Items.Insert(0, new ListItem { Value = "NULL", Text = "All Records" });
        }
    }
}

1 Comment

Items.Add will add as the last item, instead use Insert to add it to position 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.