[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Why is my listbox.selectedindex always 0?

Posted on 2007-10-12
8
Medium Priority
?
450 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?
0
Comment
Question by:rmmarsh
  • 5
  • 3
8 Comments
 
LVL 16

Expert Comment

by:gnoon
ID: 20070117
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 :)
0
 

Author Comment

by:rmmarsh
ID: 20070243
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 -----------------------------------------------------------------------------------------
0
 
LVL 16

Accepted Solution

by:
gnoon earned 1500 total points
ID: 20070295
The code above is okay. What the code you see the selectedindex is always 0?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:rmmarsh
ID: 20071011
       //-------------------------------------------------------------------------------
        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();
            }
        }
0
 

Author Comment

by:rmmarsh
ID: 20071187
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...
0
 
LVL 16

Expert Comment

by:gnoon
ID: 20071266
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.
0
 
LVL 16

Expert Comment

by:gnoon
ID: 20071271
:)
0
 
LVL 16

Expert Comment

by:gnoon
ID: 20071276
I forgot it isn't WEB! Sorry.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question