Link to home
Start Free TrialLog in
Avatar of rmmarsh
rmmarshFlag for United States of America

asked on

Why is my listbox.selectedindex always 0?

I have a listbox that I populate using a datasource I create on the fly (actually, I read a table and create the datasource).  Now, when I click on any item in that listbox, the selectedindex is always 0, which is incorrect.

Why?
Avatar of gnoon
gnoon
Flag of Thailand image

When you read the selectedindex? Read by javascript or code behind?

If javascript, it should not be happen. Javascript always does as you coding (please paste it here).

if code behind,
I guess, maybe you populate it every time the page is requested without IsPostBack checking. The populate will clear the selection of previous state automatically.

To prevent this, you need to populate it once the page is first time loaded (this.IsPostBack == false). After that, data will be cached between client/server, and the select state will be hold.

If still problem, please post you code of the ListBox tag, populate code, and populate calling .. if posible :)
Avatar of rmmarsh

ASKER

This is a C# Windows desktop program, not a web page...
code follows: ------------------------------------------------------------------------------

        private void populatePriCatalogListbox()
        {
            Cursor.Current = Cursors.WaitCursor;

            //  need to fill Primary catalog listbox
            string commandString = "select * from tCatalog WHERE AttachedTo IS NULL ORDER BY 'CatID'";
            SqlDataAdapter da = new SqlDataAdapter(commandString, bookConn);
            DataSet ds = new DataSet();
            da.Fill(ds, "tCatalog");  //  create a dataset and fill it

            lbPrimaryCatalog.DataSource = ds.Tables["tCatalog"];  //  set datasource
            lbPrimaryCatalog.DisplayMember = "CatID";

            //  fill change prices listbox also...
            lbChangePricesCat.DataSource = ds.Tables["tCatalog"];
            lbChangePricesCat.DisplayMember = "CatID";

            da.Dispose();
            ds.Dispose();
end of code -----------------------------------------------------------------------------------------
ASKER CERTIFIED SOLUTION
Avatar of gnoon
gnoon
Flag of Thailand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rmmarsh

ASKER

       //-------------------------------------------------------------------------------
        private void lbPrimaryCatalog_SelectedIndexChanged(object sender, EventArgs e)
        {
            //choosePriCatalogEntry();
            if (lbPrimaryCatalog.SelectedIndex != 0)  //  user has selected from Primary?  <------always zero!  should never be zero!
            {
                commandString = "select * from tCatalog WHERE AttachedTo IS NOT NULL AND AttachedTo = '" +
                    lbPrimaryCatalog.Text + "' order by 'CatID'";

                SqlDataAdapter da1 = new SqlDataAdapter(commandString, bookConn);
                DataSet ds1 = new DataSet();
                da1.Fill(ds1, "tCatalog");  //  create a dataset and fill it

                lbSecondaryCatalog.SelectedIndexChanged -= lbSecondaryCatalog_SelectedIndexChanged;
                lbSecondaryCatalog.DataSource = ds1.Tables["tCatalog"];
                lbSecondaryCatalog.DisplayMember = "AttachedTo";
                lbSecondaryCatalog.SelectedIndexChanged += lbSecondaryCatalog_SelectedIndexChanged;

                da1.Dispose();
                ds1.Dispose();
            }
        }
Avatar of rmmarsh

ASKER

I found the problem... the DataSource doesn't work.  When I changed it to a datareader and read each item, populating the listbox, it works like a champ...

Thanks for your help... I appreciate it...
Okay. Now the onload method should looked like this

void Page_Load(object sender,EventArgs e)
{
   if(!IsPostBack) //<--------- check here before call the populate
   {
        // call it here once the page first loaded, so the select state will be hold
        populatePriCatalogListbox();
   }
}

Isn't it? And make sure you didn't clear the select state in Page_Init method.
:)
I forgot it isn't WEB! Sorry.