Solved

Restrict access to Dropdown List values

Posted on 2009-05-15
6
379 Views
Last Modified: 2013-11-26
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
Comment
Question by:jazzcatone
  • 3
  • 2
6 Comments
 
LVL 2

Expert Comment

by:jhabas_ons
ID: 24397337
Have you considered just removing the item from the list if they are not allowed access?
0
 
LVL 2

Accepted Solution

by:
jhabas_ons earned 250 total points
ID: 24397426

      if (lblDept.Text == "Quality Assurance" || lblDept.Text == "Information Technology")

      {

        ddlComplaintstatus.Items.RemoveAt(0);

      }

Open in new window

0
 

Author Comment

by:jazzcatone
ID: 24397450
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:jazzcatone
ID: 24397486
Sorry. I didn't see your code sample you provided. I understand your comment now.
0
 
LVL 2

Expert Comment

by:jhabas_ons
ID: 24397599
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
 
LVL 2

Assisted Solution

by:krishrr
krishrr earned 250 total points
ID: 24397627
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now