I am writing a web page in ASP (using C#) that needs to take data from a Database and put it into a ListBox, then have a button retrieve the selected item and then do some stuff. The problem I am having is retrieving the values in the ListBox.
Expand|Select|Wrap|Line Numbers
- protected void Page_Load(object sender, EventArgs e)
- {
- CategoriesTableAdapter categories = new CategoriesTableAdapter();
- DataTable table1 = categories.GetCategories();
- ListBox1.DataSource = table1;
- ListBox1.DataTextField = "CategoryName";
- ListBox1.DataValueField = "CategoryID";
- ListBox1.DataBind();
- }
- protected void SubmitBtn_Click(Object sender, EventArgs e)
- {
- Label1.Text = ( "Index: " + ListBox1.SelectedIndex );
- Label2.Text = ( "Selection Text: " + ListBox1.SelectedItem );
- Label3.Text = ( "Selection Value: " + ListBox1.SelectedValue );
- }
Index: -1
Selection Text:
Selection Value:
when trying to debug and trace my problem, I found that it works perfectly fine when I manually put in values via:
Expand|Select|Wrap|Line Numbers
- <asp:ListItem Value="1" Text="Item 1"/>