• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 408
  • Last Modified:

Restrict access to Dropdown List values

Hi and thanks for taking a moment. I need to know how to restrict the availability of  a dropdown value from a dropdown list control. I want to do something like what is below, however in c# this is invalid.  All of the original values for the dropdown list control are populated from a database. I need to filter them at the code level now. Any help greatly appreciated.

if (lblDept.Text == "Quality Assurance" || lblDept.Text == "Information Technology")
        {
 
            ddlComplaintstatus.Items[0].Visible = false;
          }

Open in new window

0
jazzcatone
Asked:
jazzcatone
  • 3
  • 2
2 Solutions
 
jhabas_onsCommented:
Have you considered just removing the item from the list if they are not allowed access?
0
 
jhabas_onsCommented:

      if (lblDept.Text == "Quality Assurance" || lblDept.Text == "Information Technology")
      {
        ddlComplaintstatus.Items.RemoveAt(0);
      }

Open in new window

0
 
jazzcatoneAuthor Commented:
Hi and thanks for your reply. Certain people may need to access those values however. I know how to write the conditional ( the "if" statement). Just can't figure out how to isolate the particular value and make it invisible.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
jazzcatoneAuthor Commented:
Sorry. I didn't see your code sample you provided. I understand your comment now.
0
 
jhabas_onsCommented:
If you will not know where the item is located in the list you may be able to do something like the following:
ListItem li = ddlComplaintstatus.Items.FindByValue("Pending");
if (lblDept.Text == "Quality Assurance" || lblDept.Text == "Information Technology")
  ddlComplaintstatus.Items.Remove(li);

Open in new window

0
 
krishrrCommented:
You can add items manually with for loop to dropdown from dataset or datareader instead of directly binding them using 'DataSource'.

Method 1:
DropDownList.Items.Add("value")

Method2 :
To add items to the DropDownList, along with a Value Field, you can do something like this (the second item is the one that populates the Value:
ddl2.Items.Add(New ListItem("Item 1", "1"))

OR

You can remove the items after binding with DataSource like this:

Method 1:
ddl.Items.RemoveAt(index)

Method2:
ListItem li = dropdownlist.Items.FindByValue(strValue);
if(li != null)
    dropdownlist.Items.Remove(li);
0

Featured Post

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now