Alex Rivera | Logout

Make ListBox items have a different value than item text

Asked 2009-05-15T08:09:02.600
23

I want a ListBox full of items. Although, each item should have a different value. So when the user selects an item and presses a button, a method will be called which will use the value the select item has.

I don't want to reveal the item values to the user.

EDIT: This is not for ASP.NET, it's for a Windows Forms application. I just thought the HTML example would be easy to read.

I have the inspiration from HTML:

<form>
<input type="radio" name="sex" value="Value1" /> Male
<br />
<input type="radio" name="sex" value="Value2" /> Female
</form>

This also allows me to use different values than what the user sees.

Edit
Report

1 Answer

-1

Easy!

protected void Page_Load(object sender, EventArgs e)
    {
        llenaListBox(ListBox1, 0, 10);
    }

    private void llenaListBox(ListBox PoListBox, int PiMinimo, int PiMaximo)
    {
        int Li;
        for (Li = PiMinimo; Li <= PiMaximo; Li++)
        {
            ListItem obj = new ListItem();
            obj.Text  = Li.ToString();
            obj.Value = Li.ToString();
            PoListBox.Items.Add(obj);
        }

    }
answered 2013-05-17T16:11:57.913

Your Answer