dropdownlist选择的值是不是在变化?

时间:2014-01-23 08:24:33

标签: c# asp.net

dropdownlist选择的值不会改变。虽然字符串_month也包含值,但dropdownlist没有得到它。所有其他工作正常,但只有ddlMonth.selected值没有改变。我已经为它分配了_month值,但它没有改变原因?只有ddlMonth没有改变所有其他工作正常ddlmonth没有改变?

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

其他代码低于

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";
          }
     }
}

2 个答案:

答案 0 :(得分:1)

我认为你在ddlMonth中设置一个值,但是ddlMonth没有那个绑定值。在设置值之前尝试绑定ddlMonth中的值列表

答案 1 :(得分:0)

下次请在发布前格式化代码。 对于这个问题,我认为解决方案是将您设置SelectedValue的区域放在

if(!isPostback){...}

我建议您查看有关页面生命周期的文档,因为解释是Page_Load不仅在您第一次加载页面时执行,而且还在每次回发时执行(如果您不使用if(!isPostback){...}