Ameer Ali Shar
Ameer Ali Shar

Reputation: 54

dropdownlist selected value is not changing?

dropdownlist selected value is not changing. although the string _month also contain the value but dropdownlist is not getting it.all other are working fine but only ddlMonth.selected value is not changing.i have assigned _month value to it but its not changing why? only ddlMonth is not changing all other are working fine why ddlmonth is not changing?

if (_objMonth.Contains("Month"))
{
    string _Month = (string)_objMonth.GetData("Month");
    ddlMonth.SelectedValue = _Month; 
    ///here ddlMonth.selected value is not getting new value from _month
}

Other code is below

protected void Page_Load(object sender, System.EventArgs e)
{
   if (Page.IsPostBack)
       return;
   try
   {
       OnLoad();
       GetYears();
       if (!string.IsNullOrEmpty(ddlYear.SelectedValue))
       hYearId.Value = ddlYear.SelectedValue;
       GetPeriods(Convert.ToInt32(hYearId.Value));
       GetDepartment();
       GetSection();

       #region Get Selected login user department and section
       ddldepartment.SelectedValue = CommonMethods.UserContext.EmployeeDeparmentID;
       ddlSection.SelectedValue = CommonMethods.UserContext.EmployeeSectionID;

       #endregion

       ddldepartment_SelectedIndexChanged(null, null);
       ddlemp_SelectedIndexChanged(null, null);
       string name = Request.QueryString["id"] as string;

       #region Create Cache object

       ICacheManager _objYear = CacheFactory.GetCacheManager();//Create cache object
       ICacheManager _objMonth = CacheFactory.GetCacheManager();//Create cache object
       ICacheManager _objDepartment = CacheFactory.GetCacheManager();//Create cache object
       ICacheManager _objSection = CacheFactory.GetCacheManager();//Create cache object
       #endregion

    if (Request.QueryString["ClickTag"]!=null)
    {
       #region set Cached items
       if (Request.QueryString["ClickTag"].ToString() == "1")
       {
          if (_objYear.Contains("Year"))
          {
             string _Year = (string)_objYear.GetData("Year");
             ddlYear.SelectedValue = _Year;
          }
          if (_objMonth.Contains("Month"))
          {
             string _Month = (string)_objMonth.GetData("Month");
             ddlMonth.SelectedValue= _Month;
          }
          if (_objDepartment.Contains("Department"))
          {
             string _Department = (string)_objDepartment.GetData("Department");
             ddldepartment.SelectedValue= _Department;
          }
          if (_objSection.Contains("Section"))
          {
             string _Section = (string)_objSection.GetData("Section");
             ddlSection.SelectedValue = _Section;
          }
       }
       #endregion
    }

protected void ddlMonth_SelectedIndexChanged(object sender, EventArgs e)
{
   try
   {
      if (!string.IsNullOrEmpty(ddlMonth.SelectedValue))
      {
         hClpid.Value = ddlMonth.SelectedValue.Split(',')[0];
         Session["Startdate"] = ddlMonth.SelectedValue.Split(',')[2];
         Session["EndDate"] = ddlMonth.SelectedValue.Split(',')[3];
         ddldepartment_SelectedIndexChanged(null, null);
         ddlemp_SelectedIndexChanged(null, null);
         if (ddlSection.SelectedIndex > 0)
            ddlSection_SelectedIndexChanged(null, null);
      }
   }

void GetPeriods(int _year)
{
     IBLCalenderPeriod _bl = (IBLCalenderPeriod)SetupBLFactory.GetCalenderPeriod();

     DataSet _ds = (DataSet)_bl.GetPeriodIdsByYear(_year).GetMaster();

     _ds.Tables[0].Columns.Add("ID");

     foreach (DataRow _dr in _ds.Tables[0].Rows)
     {
          _dr["ID"] = _dr["CLP_ID"] + "," + _dr["clp_activeperiod"] + "," + _dr["CLP_DATESTART"] + "," + _dr["CLP_DATEEND"] + "";

     }
     ddlMonth.DataSource = _ds.Tables[0];
     ddlMonth.DataTextField = "CLP_DESCRIPTION";
     ddlMonth.DataValueField = "ID";
     ddlMonth.DataBind();

     foreach (DataRow _dr in _ds.Tables[0].Rows)
     {
          if (_dr["clp_activeperiod"] != null)
          if (_dr["clp_activeperiod"].ToString() == "1")
          {
             ddlMonth.SelectedValue = _dr["ID"].ToString();
             hClpid.Value = ddlMonth.SelectedValue.Split(',')[0];
             Session["Startdate"] = ddlMonth.SelectedValue.Split(',')[2];
             Session["EndDate"] = ddlMonth.SelectedValue.Split(',')[3];
             break;
          }
          else
          {
             ddlMonth.SelectedIndex = 0;
             hClpid.Value = "0";
          }
     }
}

Upvotes: 1

Views: 2147

Answers (2)

kirk
kirk

Reputation: 1007

I think you are setting a value in ddlMonth but ddlMonth do not have that binded value.. Try to bind list of values in your ddlMonth before setting a value to it

Upvotes: 1

Alessio
Alessio

Reputation: 2068

Please next time format the code before posting. For the problem I think the solution is to put the region where you set the SelectedValue inside an

if(!isPostback){...}

I suggest you to take a look to the documentation about page lifecycle, because the explanation is that the Page_Load in executed not only the first time you load a page, but also for every postback (if you don't use the if(!isPostback){...}

Upvotes: 0

Related Questions