2017-08-28 100 views
1

在我的頁面我填充GridView從後面的代碼通過設置源自定義Datatable,我從一個XML文件編譯,這樣我寫列標題和行標題。 這工作得很好,所以我在細胞中添加複選框,而以這種方式加入列:從數據表複選框不處理選中的事件

  DataTable dt = new DataTable(); 
     dt.Columns.Add(" "); 
     foreach (XmlNode xns in doc.DocumentElement.ChildNodes[0]) 
     { 
      foreach (XmlNode xn in xns) 
      { 
       string tagName = xn.Name; 

       dt.Rows.Add(tagName); 
      } 

     } 

     dt.Columns.Add("Mattina Turno 1", typeof(bool)); //this adds the checkbox 
     dt.Columns.Add("Mattina Turno 2", typeof(bool)); 
     dt.Columns.Add("Pomeriggio", typeof(bool)); 

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

我能夠在我的GridView的RowDataBound每個複選框與GridViewRowEventArgs爲e這樣:

protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e) 
    { 

      for (int i = 0; i < e.Row.Cells.Count;i++) 
      { 
       if (e.Row.Cells[i].GetType() == typeof(System.Web.UI.WebControls.DataControlFieldCell)) 
       { 
        TableCell tc = e.Row.Cells[i]; 
        if (tc.Controls.Count > 0) 
        { 
         CheckBox cb = (CheckBox)tc.Controls[0]; 
         if (cb != null) 
         { 
          cb.Enabled = true; 
          colonna = ((GridView)sender).HeaderRow.Cells[i].Text; 
          riga = e.Row.Cells[0].Text; 
          cb.CausesValidation = false; 
          cb.ID = riga + " " + colonna; 
          cb.ToolTip = riga + " " + colonna; 
          cb.AutoPostBack = true; 
          cb.CheckedChanged += new EventHandler(Cb_CheckedChanged); 
          cb.Attributes.Add("runat", "server"); 
         } 
        } 
       } 
      } 

    } 

但是,當我嘗試處理複選框的檢查事件沒有任何反應。 checkchanged應該調用Cb_CheckedChanged但沒有任何反應。

這Cb_CheckChanged:

 private void Cb_CheckedChanged(object sender, EventArgs e) 
    { 
     Cliccato.Text = ((CheckBox)sender).ID.ToString(); 
     System.Diagnostics.Debug.Write(((CheckBox)sender).ToolTip); 
    } 

將AutoPostBack似乎工作,因爲當我點擊複選框的頁面刷新,但它不處理任何事件...... 請幫助我,我真的需要你的幫助!

+0

可能的複製的(https://stackoverflow.com/questions/17275166/checkboxes-in-datagridview-not-firing-cellvaluechanged-event) – MatSnow

+0

雖然它可能是同一個問題,你看他們找到了正確的答案?這是一個'13問題,我可以確保它不一樣。請不要標記爲重複。 –

回答

0

動態地GridView控件像添加複選框:

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) 
{ 
    // check if it's not a header and footer 
    if (e.Row.RowType == DataControlRowType.Row) 
    { 
     CheckBox chk = new CheckBox(); 

     chk.AutoPostBack = true; 

     // add checked changed event to checkboxes 
     chk.CheckedChanged += new EventHandler(chk_CheckedChanged); 

     e.Row.Cells[1].Controls.Add(chk); // add checkbox to second column 
    } 
} 

對於RowDataBound獲取文本從下面的代碼的每一行使用的細胞:在DataGridView的複選框沒有觸發CellValueChanged事件]

if (e.Row.RowType == DataControlRowType.Row) 
{ 
    // assuming there is label in first cell, you cast it that you want 
    string cellText = (e.Row.Cells[0].FindControls("Label1") as Label).Text; 
} 
0

您沒有在代碼中設置runat服務器屬性,因此當複選框處於選中狀態時,沒有任何反應。

也許你可以嘗試這樣的事:

cb.Attributes.Add("runat", "server"); 

你也應該放置一個破發點,如果塊內,並檢查代碼是否進入控制初始化部分。