public void DataGridAllCheck(DataGridItems it,CheckBox c)
{
foreach(DataGridItem i in it)
{
CheckBox cb=(CheckBox)i.FindControl(c);
if(cb.Checked)
{
cb.Checked=false;
}
else
{
cb.Checked=true;
}
}
}
{
foreach(DataGridItem i in it)
{
CheckBox cb=(CheckBox)i.FindControl(c);
if(cb.Checked)
{
cb.Checked=false;
}
else
{
cb.Checked=true;
}
}
}
这里的c名称必须和你datagrid中checkbox的ID相同。
不过逻辑好象是反选,而不一般的全选或取消来着
在就是判断前请确认对象存在
if(cp!=null)
{
//执行反选操作
}
{
CheckBox chk = (CheckBox)item.FindControl("CheckBox1");
if ( chk.Checked )
{
}
else
{
}
}
public string GetCtrlSelValue(string CtrName,DataGrid ctrl)
{
string str_Value=""; foreach(DataGridItem dl in ((DataGrid)ctrl).Items)
{
CheckBox chk=(CheckBox)dl.FindControl(CtrName);
if(chk.Checked)
{
str_Value=str_Value+","+ctrl.DataKeys[dl.ItemIndex].ToString();
}
}
if (str_Value!="")
{
int i=str_Value.Length;
str_Value=str_Value.Substring(1,i-1);
}
return str_Value;
}
{
foreach(DataGridItem i in it)
{
if(item.ItemType != ListItemType.Header && item.ItemType != ListItemType.Footer)
{
CheckBox cb=(CheckBox)i.FindControl(c);
if(cb != null && cb.Checked)
{
cb.Checked=false;
}
else
{
cb.Checked=true;
}
}
}
}
public void DataGridAllCheck(DataGridItems it,CheckBox c)
{
foreach(DataGridItem i in it)
{
if(i.ItemType != ListItemType.Header && i.ItemType != ListItemType.Footer)
{
CheckBox cb=(CheckBox)i.FindControl(c);
if(cb != null && cb.Checked)
{
cb.Checked=false;
}
else
{
cb.Checked=true;
}
}
}
}
Private Sub ckb_all_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ckb_all.CheckedChanged
Dim oDataGridItem As DataGridItem
Dim chkExport As System.Web.UI.WebControls.CheckBox For Each oDataGridItem In Me.dg_main.Items
chkExport = oDataGridItem.FindControl("cbk")
chkExport.Checked = Me.ckb_all.Checked
Next
End Sub我是在一个checkbox的postback里面做的,道理是一样的,你只过不是再把datagrid和checkbox传进去