Solved

Restrict access to Dropdown List values

Posted on 2009-05-15
6
384 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

840 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