Last post Jan 24, 2012 03:10 AM by Decker Dong - MSFT
Jan 22, 2012 12:53 PM|nanonerd|LINK
I have a listbox fed by a sqldatasource. When a button is clicked, get the selected item in the listbox. When I set it up via controls, etc., it all works great.
PROBLEM: When I use code to bind the listbox to a datasource, the items show up in the listbox, but when I select an item and click the button, the "value" is blank ... No good.
LBClans.DataSourceID = null;
LBClans.DataSource = SqlAllClans
protected void ButtonViewClan_Click(object sender, EventArgs e)
Label1.Text = LBClans.SelectedValue.ToString();
THINGS TRIED: I tried a great many things ... I even tried adding the following:
LBClans.DataTextField = "name";
LBClans.DataValueField = "name";
But no dice. I don't understand ... since I can see the items, it appears that the databind() is correct. However, there is no value when I try to get the selected value.
- Baffled ...
Jan 22, 2012 12:59 PM|MetalAsp.Net|LINK
You're binding in page_load yes? If so, make sure you put that binding code inside of an if (!IsPostBack) block and you'll be set.
Jan 24, 2012 03:10 AM|Decker Dong - MSFT|LINK
I think another way is to use something datasource like ObjectDataSource，SqlDataSource。According to the wizard，it iwll be easy for you to bind with the ListBox with the DataSource。