Click here to Skip to main content
15,901,879 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a 3 dropdownlist control with a selectedindexchanged event that fires correctly. However, when you select an item from the list the index value that is returned to the selectedindexchanged event does not change; the list box pops back to the first item in the list. Any help would be appreciated. ~Dharmendra~
C#
public partial class Production : System.Web.UI.Page
    { 
        EmployeeQuotientCL.Production _production = null;
        DataSet dsNatureOfWork = new DataSet();
        DataSet dsProjectRegion = new DataSet();
        DataSet dsCountyDetails = new DataSet();
        DataSet dsWorkType = new DataSet();
        DataSet dsTask = new DataSet();
       

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                 string userEcode=Convert.ToString(Session["UserID"]); 
                _production = new EmployeeQuotientCL.Production();                       
                dsNatureOfWork = _production.GetNatureOfWork();
                if (dsNatureOfWork.Tables[0].Rows.Count > 0)
                {
                    BindDdlNatureOfWork(dsNatureOfWork);
                }
                else
                {
                }
            }            

        }


        
        public void BindDdlNatureOfWork(DataSet dsNatureOfWork)
        {
            ddlNatureofWork.DataSource = dsNatureOfWork.Tables[0];
            ddlNatureofWork.DataTextField = "NatureOfWorkName";
            ddlNatureofWork.DataValueField = "NatureOfWorkID";
            ddlNatureofWork.DataBind();
            ddlNatureofWork.Items.Insert(0, "--Select Nature of Work--");
        }

        public void FillRegionProject(int NatureOfWorkID)
        {
            if ((NatureOfWorkID != null) || (NatureOfWorkID != 0))
            {
                _production = new EmployeeQuotientCL.Production();
                dsProjectRegion = _production.GetProjectRegion(NatureOfWorkID);
                if (dsProjectRegion.Tables[0].Rows.Count > 0)
                {
                    ddlRegionProjectName.DataSource = dsProjectRegion.Tables[0];
                    ddlRegionProjectName.DataTextField = "RegionProjectName";
                    ddlRegionProjectName.DataValueField = "RegionProjectID";
                    ddlRegionProjectName.DataBind();
                    ddlRegionProjectName.Items.Insert(0, "--Select Region/Project--");

                }
                else
                {
                }
            }

        }

        protected void ddlRegionProjectName_SelectedIndexChanged(object sender, EventArgs e)
        {
            int RegionProjectID = Convert.ToInt32(ddlRegionProjectName.SelectedValue.ToString());
            FillCounty(RegionProjectID);
            ddlRegionProjectName.SelectedIndex = 0;
        
        }

        public void FillCounty(int regionprojectID)
        {
            if ((regionprojectID != null) || (regionprojectID != 0))
            {
                _production = new EmployeeQuotientCL.Production();
                dsCountyDetails = _production.GetCounty(regionprojectID);
                if (dsCountyDetails.Tables[0].Rows.Count > 0)
                {
                    ddlCountyName.DataSource = dsCountyDetails.Tables[0];
                    ddlCountyName.DataTextField = "CountyName";
                    ddlCountyName.DataValueField = "CountyID";
                    ddlCountyName.DataBind();
                    ddlCountyName.Items.Insert(0, "--Select County--");

                }
                else
                {
                }
            }
        }

        protected void ddlNatureofWork_SelectedIndexChanged(object sender, EventArgs e)
        {
            int NowID = Convert.ToInt32(ddlNatureofWork.SelectedValue.ToString());
            FillRegionProject(NowID);
            ddlRegionProjectName.SelectedIndex = 0;
        }
   
    }
}
Posted
Updated 11-May-12 2:37am
v2
Comments
Herman<T>.Instance 11-May-12 8:37am    
Is AutoPostBack set to true?
Sandeep Mewara 11-May-12 8:38am    
Even if it is set, its OK as he has the IsPostBack page property checked thus avoiding the re-bind.
Dharmenrda Kumar Singh 11-May-12 8:39am    
Yes , It is set to true for every dropdown.
Sandeep Mewara 11-May-12 8:37am    
Where is Listbox and related code here? All I see is dropdownlist code.
Dharmenrda Kumar Singh 11-May-12 8:41am    
Sorry it is the list values binded to the dropdown .By mistake i had mentioned list box.

The issue is solved as by mistake i added ddlRegionProjectName.SelectedIndex = 0; and ddlRegionProjectName.SelectedIndex = 0;in every selectedindexChanged event.
 
Share this answer
 
Hello Dharmendra,

what does this section in your code?
C#
else
{
}


it is not a good coding practice, remove unwanted code.

please remove the below code and try again

C#
ddlRegionProjectName.SelectedIndex = 0;


Regards
Sebastian
 
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