title: Problem with Auto Generated Dropdownlist column in Gridview ASP.Net
body: <pre><code>protected void gridChamCong_OnRowDataBound(object sender, GridViewRowEventArgs e)
      {
          List&lt;DateTime&gt; NgayNghi = new List&lt;DateTime&gt;();
          NgayNghi = oNgayNghi.load_ListNgayNghi();
          int ngayle = 0;
          if (e.Row.RowType == DataControlRowType.DataRow)
          {
  
              e.Row.Cells[2].Text = txtCongChuan.Text;                
              e.Row.Cells[3].Text = txtCongChuan.Text;                
              e.Row.Cells[4].Text = txtCongChuan.Text;
              e.Row.Cells[5].Text = txtCongChuan.Text;
  
              System.Web.UI.WebControls.TextBox txtCa3 = new System.Web.UI.WebControls.TextBox();
              txtCa3.ID = "txtCa3";
              txtCa3.Text = "0";
              txtCa3.Width = 30;
              e.Row.Cells[6].Controls.Add(txtCa3); 
  
              int songay = 0;
              DateTime denngay = DateTime.ParseExact(dateDenNgay.Text, "yyyy-MM-dd", null);
              DateTime tungay = DateTime.ParseExact(dateTuNgay.Text, "yyyy-MM-dd", null);
              songay = ((int)(denngay - tungay).TotalDays) + 1;
              for (int i = 0; i &lt; songay; i++)
              {
                  DropDownList ddlKiHieuChamCong = new DropDownList();
                  ddlKiHieuChamCong.ID = "ddlKiHieuChamCong";
                  System.Data.DataTable ddlKyHieu = new System.Data.DataTable();
                  ddlKyHieu = oKyHieuChamCong.GetDataDDLKyHieuChamCong();
                  ddlKiHieuChamCong.DataSource = ddlKyHieu;
                  ddlKiHieuChamCong.DataTextField = ddlKyHieu.Columns[1].ColumnName;
                  ddlKiHieuChamCong.DataValueField = ddlKyHieu.Columns[0].ColumnName;
                  ddlKiHieuChamCong.DataBind();
                  //ddlKiHieuChamCong.AutoPostBack = true;
                  ddlKiHieuChamCong.SelectedIndexChanged += ddlKiHieuChamCong_SelectedIndexChanged;
                  ddlKiHieuChamCong.Enabled = true;
                  ddlKiHieuChamCong.EnableViewState = true;
                  DateTime ngayxet = tungay.AddDays(i);
                  if (ngayxet.DayOfWeek == DayOfWeek.Saturday || ngayxet.DayOfWeek == DayOfWeek.Sunday)
                      ddlKiHieuChamCong.SelectedIndex = 9;
                  else if (NgayNghi.Exists(x =&gt; x.Date == ngayxet.Date))
                  {
                      ddlKiHieuChamCong.SelectedIndex = 5;
                      ngayle++;
                  }
                  else
                      ddlKiHieuChamCong.SelectedIndex = 0;
                  e.Row.Cells[7].Text = ngayle+"";
                  e.Row.Cells[i + 8].Controls.Add(ddlKiHieuChamCong);
              }
          }
      }
  protected void btnTao_Click(object sender, EventArgs e)
      {
          List&lt;DateTime&gt; NgayNghi = new List&lt;DateTime&gt;();
          NgayNghi = oNgayNghi.load_ListNgayNghi();
          if (gridChamCong.Columns.Count &gt; 0)
              gridChamCong.Columns.Clear();
  
          BoundField bfield = new BoundField();
          bfield.HeaderText = "Id";
          bfield.DataField = "ID";
          gridChamCong.Columns.Add(bfield);
  
          BoundField tName = new BoundField();
          tName.HeaderText = "Nhân sự";
          tName.DataField = "Tên Nhân Viên";
          gridChamCong.Columns.Add(tName);
  
          TemplateField tCountDate = new TemplateField();
          tCountDate.HeaderText = "Ngày công";
          gridChamCong.Columns.Add(tCountDate);
  
          TemplateField tNgayLuongHeSo = new TemplateField();
          tNgayLuongHeSo.HeaderText = "LHS";
          gridChamCong.Columns.Add(tNgayLuongHeSo);
  
          TemplateField tNgayLuongCanBan = new TemplateField();
          tNgayLuongCanBan.HeaderText = "LCB";
          gridChamCong.Columns.Add(tNgayLuongCanBan);
  
          TemplateField tNgayAnGiuaCa = new TemplateField();
          tNgayAnGiuaCa.HeaderText = "AGC";
          gridChamCong.Columns.Add(tNgayAnGiuaCa);
  
          TemplateField tNgayCa3 = new TemplateField();
          tNgayCa3.HeaderText = "Ca3";
          gridChamCong.Columns.Add(tNgayCa3);
  
          TemplateField tNgayPhep = new TemplateField();
          tNgayPhep.HeaderText = "Lễ/Phép";
          gridChamCong.Columns.Add(tNgayPhep);
  
          int songay = 0;
          DateTime denngay = DateTime.ParseExact(dateDenNgay.Text, "yyyy-MM-dd", null);
          DateTime tungay = DateTime.ParseExact(dateTuNgay.Text, "yyyy-MM-dd", null);
          if (denngay &gt;= tungay)
          {
              songay = ((int)(denngay - tungay).TotalDays) + 1;
              int congchuan = connectDatabase.BusinessDaysUntil(tungay, denngay,NgayNghi);
              txtCongChuan.Text = congchuan.ToString();
              for (int i = 0; i &lt; songay; i++)
              {
                  TemplateField tDate = new TemplateField();
                  tDate.HeaderText = (tungay.AddDays(i)).ToString("dd/MM");
                  gridChamCong.Columns.Add(tDate);
              }
          }
          else
          {
              lbLoi.ForeColor = Color.Red;
              lbLoi.Text = "Đến ngày phải lớn hơn Từ ngày";
          }
  
          BindGridChamCong();
      }
  
      //----------------------------------------CHẤM CÔNG------------------------------
  
      protected void btnLuu_Click(object sender, EventArgs e)
      {
          DateTime denngay = DateTime.ParseExact(dateDenNgay.Text, "yyyy-MM-dd", null);
          DateTime tungay = DateTime.ParseExact(dateTuNgay.Text, "yyyy-MM-dd", null);
          DateTime ngaytao = DateTime.ParseExact(dateNgayTao.Text, "yyyy-MM-dd", null);
          //get id phiếu lớn nhất
          txtPhieuID.Text = oPhieuChamCong.getMaxID() + "";
          //-------------------------------------------------------------------------------------------------
  
          //Insert Phiếu chấm công
          oPhieuChamCong.insertPhieuChamCong(txtTenPhieu.Text, Int32.Parse(ddlDonvi.SelectedValue), tungay, denngay, 1, Int32.Parse( connectDatabase.ID_Nhanvien)
              , ngaytao, txtGhiChu.Text);            
          //-------------------------------------------------------------------------------------------------------
  
          //Insert phiếu chấm công tổng
          for (int i = 0; i &lt; gridChamCong.Rows.Count; i++)
          {
              string mang = "";
              int ngaycongtinhluong = Int32.Parse(txtCongChuan.Text.Trim());
              int angiuaca = ngaycongtinhluong;
              int lhs = ngaycongtinhluong;
              int lcb = ngaycongtinhluong;
              int phep = 0;
              for (int j = 0; j &lt; gridChamCong.Columns.Count-8 ; j++)
              {
                  DateTime ngayxet = tungay.AddDays(j);
                  if (ngayxet.DayOfWeek != DayOfWeek.Saturday &amp;&amp; ngayxet.DayOfWeek != DayOfWeek.Sunday)
                  {
                       System.Web.UI.WebControls.DropDownList  ddlChamcong = new  System.Web.UI.WebControls.DropDownList ();
                      int slcontrol = gridChamCong.Rows[i].Cells[j + 8].Controls.Count;
                      ddlChamcong = ( System.Web.UI.WebControls.DropDownList)gridChamCong.Rows[i].Cells[j+8].FindControl("ddlKiHieuChamCong");
                      string congngay = ddlChamcong.Text.Trim();
                      mang = mang + ngayxet.Date.ToString("yyyy-MM-dd") + "," + congngay;
                      mang = mang + ";";
                      if (!congngay.Equals("P") &amp;&amp; !congngay.Equals("X") &amp;&amp; !congngay.Equals("L"))
                      {
                          lhs--;
                          angiuaca--;
                          ngaycongtinhluong--;
                          lcb--;
                      }
                      else if (congngay.Equals("P") || congngay.Equals("L"))
                      {
                          lhs--;
                          angiuaca--;
                          ngaycongtinhluong--;
                          phep++;
                      }
                  }
              }
              System.Web.UI.WebControls.TextBox txtECustCode = (System.Web.UI.WebControls.TextBox)gridChamCong.Rows[i].Cells[6].FindControl("txtCa3");
              int ca3 = Int32.Parse(txtECustCode.Text);
              int id_phieu = Int32.Parse(txtPhieuID.Text.Trim());
              int id_nv = Int32.Parse(gridChamCong.Rows[i].Cells[0].Text);
  
              oPhieuChamCongTong.insertPhieuChamCongTong(id_phieu, id_nv 
                  , lhs, lcb, angiuaca, ngaycongtinhluong,ca3, phep, mang, "");
          }            
          //----------------------------------------------------------------------------------------------------
  
          //ghi log
          connectDatabase.insert_Log("updatePhieuChamCong", connectDatabase.TenTK, "Cập nhật/Thêm phiếu chấm công  "+ txtPhieuID.Text + txtTenPhieu.Text + " Đơn vị: " + ddlDonvi.Text, DateTime.Now);
  
          Response.Redirect("~/Quanly/ChiTietChamCong.aspx");
      }
  </code></pre>
  
  <p></p>
  
  <p>I have a problem when I click <code>btnLuu_Click</code>, <code>gridChamCong.Rows[i].Cells[6].FindControl("txtCa3")</code>  and 
  <code>gridChamCong.Rows[i].Cells[j+8].FindControl("ddlKiHieuChamCong")</code>  return null. If I uncomment <code>ddlKiHieuChamCong.AutoPostBack = true</code> , When I change selected index of <code>ddlKiHieuChamCong</code>, all controls in columns generated become null.</p>
  
  <p>Inside the <code>OnRowDataBound</code> event of the GridView, the process of adding controls like TextBox, DropDownLists</p>