Johnny Bones
Johnny Bones

Reputation: 8404

2-column dropdownlist with one column hidden

I'm hoping this is a quick one. Sorry, still an ASP/C# n00b and I can't seem to find an example.

What I want to do is have a dropdownlist on an ASP page. I want the list to display 2 values; Benefit Type and Priority. However, the dropdownlist is used as a filter for the data to display, so my field names in the table are BENTYP and PRIO. So, the user will see "Benefit Type", but the code-behind will be able to read "BENTYP". It's sort of like a 2-column combo box with one column hidden.

Make sense? I know this is a snap in Access, I can't imagine it's too hard in ASP but I just don't have the experience yet. Also, if you would be so kind, can you tell me how the code-behind would read the text in the "hidden" column?

EDIT: Just to be clear, the dropdownlist would look something like this:

Column1 (visible) Column2 (invisible)

Benefit Type ---- BENTYP

Priority ----------- PRIO

Upvotes: 0

Views: 1724

Answers (2)

Adam Tal
Adam Tal

Reputation: 5961

What you've asked is exactly the functionality of the dropdown, it has a "Value" and a "Text" for each item it contains, just set the value as your second column and your text as the first.

From the code behind you can add it like so:

ListItem item = new ListItem("text (first column)", "value (second column)");
item.Selected = true; // whatever you want here
yourDropdownList.Items.Add(item);

To get the selected item in the code behind (the ListItem object) use:

var item = yourDropdownList.SelectedItem;
var text = item.Text;
var val = item.Value;

Upvotes: 0

AAlferez
AAlferez

Reputation: 1502

           <asp:DropDownList id="List"
                AutoPostBack="True"
                OnSelectedIndexChanged="Selection_Change"
                runat="server">

              <asp:ListItem Value="BENTYP"> Benefit Type </asp:ListItem>
              <asp:ListItem Value="PRIO"> Priority </asp:ListItem>

           </asp:DropDownList>

From code behind you just have to access the thing you want:

       string item1 = List.DataTextField.ToString();
       string item2 = List.DataValueField.ToString(); 

Hope it helps.

Founded in MSDN

Upvotes: 1

Related Questions