Hello, I'm a Java developer but brand new to ASP (i have done some of the tutorials) so I have some ASP programming background however I cannot find the cause or the fix to this problem.
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.
If it matters, I am formating my code using a class (.asxp.cs file) to run the code. I tried putting it all into one file and it has the same results. When I run the code, my ListBox is populated exactly the way I need it to be. However, when I select something and click the button that fires the SubmitBtn_Click function, it always displays the exact same thing:
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:
Any help at all would be greatly appreciated. If I missed anything important, let me know.
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.
Code:
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:
Code:
<asp:ListItem Value="1" Text="Item 1"/>