[此贴子已经被作者于2006-11-17 20:36:54编辑过]
你是要它前台響應事件還是後台響應事件啊
AutoPostBack 有沒有設置好啊
例:
private void grdServer_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Header)
{
CheckBox chk = (CheckBox)e.Item.FindControl("chkAllServer");
// 给页眉上的CheckBox添加出发事件
chk.CheckedChanged +=new EventHandler(chk_CheckedChanged);
}
}
// 得到指定DataGrid的题头的CheckBox对象
private CheckBox GetHeaderCheckBox(DataGrid grd)
{
CheckBox chk = null;
foreach (DataGridItem i in grd.Controls[0].Controls)
{
if(i.ItemType == ListItemType.Header)
{
chk = (CheckBox)i.FindControl("chkAllServer");
break;
}
}
return chk;
}
private void chk_CheckedChanged(object sender, System.EventArgs e)
{
CheckBox chk = this.GetHeaderCheckBox(this.grdServer);
foreach (DataGridItem i in this.grdServer.Items)
{
CheckBox inChk = (CheckBox)i.FindControl("CheckBox2");
inChk.Checked = chk.Checked;
}
这个是DataGrid的事件吧,我用的是GridView,我改成下面针对GridView(不知道改得对不对)的后还是不行
public void chk_CheckedChanged(object sender, System.EventArgs e)
{
CheckBox chk = this.GetHeaderCheckBox(this.GridView1);
foreach (GridViewRow i in this.GridView1.Rows)
{
CheckBox inChk = (CheckBox)i.Cells[0].Controls[1];
inChk.Checked = chk.Checked;
}
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType.ToString().Equals("Header"))
{
this.txt_code.Text = e.Row.RowType.ToString();
CheckBox chk = (CheckBox)e.Row.Cells[0].Controls[1];
chk.CheckedChanged += new EventHandler(chk_CheckedChanged);
}
}