0
votes

I have a listBox that is presenting a list of string values in a UI.

These string values are presented like -

value|1
value|2
value|3

//etc

Where the integer value is the string ID stored in the database.

I have used a split function on the next page '|' to get the string and ID when its passed through for other methods.

Is there anyway I can hide the ID value in the UI however still have it present in the listBox so it is available on the query string?

The listBox is populated like this -

foreach (string x in values)
{

    ListBox3.Items.Add(x); // I want to hide the '|1' value in the listBox

}

EDIT I want to hide the ID of the value however have it available in the query string as the split function is happening on the next page. On original page there is a button like so -

protected void Button6_Click(object sender, EventArgs e) {

        if (ListBox3.SelectedItem != null)
        {
            Response.Redirect("About.aspx?term=" + ListBox3.SelectedItem);

        }
    }

Where ListBox3.SelectedItem is value|1 then on the next page I am splitting this. So I need this full sting to pass on the query string however only want the value part visible in the listBox.

4
see edits, the full string has to be available to pass through to the next page therefore cannot use the split function here as the selected.item needs the ID. I only want to hide the ID in the UI with it still being available to pass through. - Ebikeneser

4 Answers

4
votes

Add a new item, setting the text and the value before hand:

var split = x.Split('|');
var item = new ListItem(split[0], split[1]);
ListBox3.Items.Add(item);
2
votes

I'd add the ID to the "Value" property of the listbox and set the actual display value to the "Text" property.

1
votes

How about using the Dictionary instead of using list of strings?!

You can simply keep the Id as a dictionary key and display the value to the user.

0
votes

Put a value into a value property and text to display in text property:

foreach (string x in values)
{

    ListBox3.Items.Add(new ListItem(x.Split('|')[0], x));
}

Where first parameter of ListItem is a text that should be displayed and second is a value which will be passed to your code on postback