在我的页面我填充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似乎工作,因为当我点击复选框的页面刷新,但它不处理任何事件...... 请帮助我,我真的需要你的帮助!
可能的复制的(https://stackoverflow.com/questions/17275166/checkboxes-in-datagridview-not-firing-cellvaluechanged-event) – MatSnow
虽然它可能是同一个问题,你看他们找到了正确的答案?这是一个'13问题,我可以确保它不一样。请不要标记为重复。 –