rmmarsh
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?
Why?
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(commandStri ng, bookConn);
DataSet ds = new DataSet();
da.Fill(ds, "tCatalog"); // create a dataset and fill it
lbPrimaryCatalog.DataSourc e = ds.Tables["tCatalog"]; // set datasource
lbPrimaryCatalog.DisplayMe mber = "CatID";
// fill change prices listbox also...
lbChangePricesCat.DataSour ce = ds.Tables["tCatalog"];
lbChangePricesCat.DisplayM ember = "CatID";
da.Dispose();
ds.Dispose();
end of code -------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---
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(commandStri
DataSet ds = new DataSet();
da.Fill(ds, "tCatalog"); // create a dataset and fill it
lbPrimaryCatalog.DataSourc
lbPrimaryCatalog.DisplayMe
// fill change prices listbox also...
lbChangePricesCat.DataSour
lbChangePricesCat.DisplayM
da.Dispose();
ds.Dispose();
end of code --------------------------
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
//------------------------ ---------- ---------- ---------- ---------- ---------- -----
private void lbPrimaryCatalog_SelectedI ndexChange d(object sender, EventArgs e)
{
//choosePriCatalogEntry();
if (lbPrimaryCatalog.Selected Index != 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(commandStri ng, bookConn);
DataSet ds1 = new DataSet();
da1.Fill(ds1, "tCatalog"); // create a dataset and fill it
lbSecondaryCatalog.Selecte dIndexChan ged -= lbSecondaryCatalog_Selecte dIndexChan ged;
lbSecondaryCatalog.DataSou rce = ds1.Tables["tCatalog"];
lbSecondaryCatalog.Display Member = "AttachedTo";
lbSecondaryCatalog.Selecte dIndexChan ged += lbSecondaryCatalog_Selecte dIndexChan ged;
da1.Dispose();
ds1.Dispose();
}
}
private void lbPrimaryCatalog_SelectedI
{
//choosePriCatalogEntry();
if (lbPrimaryCatalog.Selected
{
commandString = "select * from tCatalog WHERE AttachedTo IS NOT NULL AND AttachedTo = '" +
lbPrimaryCatalog.Text + "' order by 'CatID'";
SqlDataAdapter da1 = new SqlDataAdapter(commandStri
DataSet ds1 = new DataSet();
da1.Fill(ds1, "tCatalog"); // create a dataset and fill it
lbSecondaryCatalog.Selecte
lbSecondaryCatalog.DataSou
lbSecondaryCatalog.Display
lbSecondaryCatalog.Selecte
da1.Dispose();
ds1.Dispose();
}
}
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...
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.
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.
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 :)