locked
Databind() - value lost when I code the bind RRS feed

  • Question

  • User464428786 posted

    Hi,

    I'm baffled.

    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
    LBClans.DataBind();

    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 ...

    Sunday, January 22, 2012 12:53 PM

Answers

  • User-1360095595 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, January 22, 2012 12:59 PM

All replies

  • User-1360095595 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, January 22, 2012 12:59 PM
  • User3866881 posted

    Hello nanoerd:)

    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。

    Reguards!

    Tuesday, January 24, 2012 3:10 AM