Click here to Skip to main content
16,004,507 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

I have a dropdownlist, every time I select an option it refreshes to the first selected value. What might be the problem?
protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                populateDDlEmployeeIDOnpageLoad();
                loadgrdShowSelectedappointmentOnPageLoad();

            }

        }


        #region Load ddlEmployeeID On page Load
        public void populateDDlEmployeeIDOnpageLoad()
        {
            ArrayList staffList = new ArrayList();
            systemBusinessLayer = new BusinessLayer();
            
            staffList = systemBusinessLayer.loadEmployeesWithAppointments(date);

            ddlEmployeeID.DataSource = staffList;
            ddlEmployeeID.DataTextField = "EmployeeName1";
            ddlEmployeeID.DataValueField = "EmpRecNumber1";
            ddlEmployeeID.DataBind();
            ddlEmployeeID.Items.Insert(0, ".:Select Employee:.");



        }
        #endregion


        #region A method to Load grdShowSelectedappointment On Page Load with all appoinments
        protected void loadgrdShowSelectedappointmentOnPageLoad()
        {
            string date = DateTime.Now.ToShortDateString();
            DataTable dt = new DataTable();
            systemBusinessLayer = new BusinessLayer();

            //  dt = systemBusinessLayer.grdLoadSpecificEmployeeOnPageLoad(date);
            dt = systemBusinessLayer.GetAllAppointments(date);

            grdShowSelectedappointment.DataSource = dt;
            grdShowSelectedappointment.DataBind();

        }
       
        #endregion



C#
public void getEmployeeID()
       {
           dTable = new DataTable();
           systemBusinessLayer = new BusinessLayer();
           dTable = systemBusinessLayer.GetAllEmployeesInfo();
           foreach (DataRow dRow in dTable.Rows)//Go through each row inside datatable
           {
               string fullname = dRow["Fullname"].ToString();
               if (ddlEmployeeID.SelectedItem.Text == fullname)
               {
                   Session["EmpRecNumber"] = dRow["EmpRecNumber"].ToString();
               }
           }
       }
       protected void ddlEmployeeID_SelectedIndexChanged(object sender, EventArgs e)
       {
           getEmployeeID();
           if (DatePicker.SelectedDate.ToShortDateString() == "1/1/0001")//if date is not selected
           {
               if (!Page.IsPostBack)
               {
                   MessageBox.Show(Session["EmpRecNumber"].ToString());
               }
           }
           else
           {
               MessageBox.Show("Invalid selection");
           }
       }
Posted
Updated 23-Jul-11 2:39am
v2

Set property of AutoPostBack="True" of DropDownList Control to stop refreshing it
 
Share this answer
 
Comments
Anele Ngqandu 23-Jul-11 8:59am    
it is true but stil doing same thing
Go to your .aspx page and set the dropdownlist's property as Autopostback="false"
 
Share this answer
 
Comments
Anele Ngqandu 23-Jul-11 9:15am    
no that dont work to,i need the postback to be true
thatraja 23-Jul-11 12:21pm    
Did you tried that? Autopostback="false"?
Anele Ngqandu 25-Jul-11 2:59am    
Hi sory for late response..yes i did try, but i needed it to be true,but i already did found the solution,it was my stored procedure and my class
You use break point in you page load and dropdownlist selected index changed event. and watch which point work and value of those point.
 
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