Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
i am populating two drop downs from the values in the first it works fine but when i put the other two drops index to zero the all default to zero


C#
protected void ddlLifeCover_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlDisabilityCover.Enabled = true;
            ddlDisabilityCover.Items.Clear();
            ddlDreadDiseaseCover.Enabled = true;
            ddlDreadDiseaseCover.Items.Clear();


            int selection = int.Parse(ddlLifeCover.SelectedItem.Value);

            foreach (ListItem lis in ddlLifeCover.Items)
            {
                if (int.Parse(lis.Value) <= selection)
                {
                    ddlDisabilityCover.Items.Add(lis);
                    ddlDreadDiseaseCover.Items.Add(lis);

                }

                ddlDisabilityCover.SelectedIndex = 0;
            }


               }

            }
Posted
Comments
Christian Amado 30-Aug-12 12:04pm    
WinForms, ASP.NET? WPF, Silverlight?
Thomas Daniels 30-Aug-12 12:09pm    
Is it in ASP.NET, Windows Forms, or WPF?
Dasaradhi_r 30-Aug-12 12:46pm    
I guess it is a web application going by the code (It is a drop down list).
But, it is difficult to understand what your problem is?
Give details of data loaded into the dropdown list. That can help in understanding the issue.

Regardless of the framework, changing the items in a drop list, resets the selected index. If you must add or remove items, store the selected index first and reset it when you're done.
 
Share this answer
 
Comments
Sandeep Mewara 30-Aug-12 12:42pm    
Based on the code, it's object addition and thus reference call makes it reset.
when i put the other two drops index to zero the all default to zero
This is because you are dealing with objects here (reference setting and not value setting).

See:
C#
ddlDisabilityCover.Items.Add(lis); 
ddlDreadDiseaseCover.Items.Add(lis);

You found a ddlLifeCover.Item which you add to other items. It's a reference type connection. Thus, in actual you are referring to one list item in all the three leading to reset on setting other.
 
Share this answer
 
if it is in ASP.NET then PostBack is happening.
if(!isPostBack)
{
}

is required
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900