We help IT Professionals succeed at work.

New podcast episode! Our very own Community Manager, Rob Jurd, gives his insight on the value of an online community. Listen Now!

x

Why is my listbox.selectedindex always 0?

rmmarsh
rmmarsh asked
on
852 Views
Last Modified: 2012-06-27
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?
Comment
Watch Question

Commented:
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 :)

Author

Commented:
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 -----------------------------------------------------------------------------------------
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
       //-------------------------------------------------------------------------------
        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();
            }
        }

Author

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

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

Commented:
:)

Commented:
I forgot it isn't WEB! Sorry.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.