怎样获取datagrid模板列中的checkbox是否被选中 在做批量删除程序时,在datagrid中添加了模板列,模板列里面有一个checkbox,怎么样来获取这个box是否被选中。或者哪位有更好的批量删除方法,给我说说,谢谢。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 checkbox chk=(checkbox)datagrid1.items[0].cells[0].findcontrol("checkbox1");if(chk.checked==true) //delete;C#中注意大小写 获取这个box是否被选中,然后删除选中的foreach(DataGridItem thisItem in DataGrid1.Items) { if(((CheckBox)thisItem.Cells[0].FindControl("ItemCheckBox")).Checked) { arrID += DataGrid1.DataKeys[thisItem.ItemIndex].ToString() + ","; } } if (arrID.Length > 1) { arrID = arrID.Substring(0,arrID.Length-1); // 数据库连接 SqlConnection myConnection = new SqlConnection(sconn); string SqlCmd = "Delete From table Where ...."; SqlCommand myCommand = new SqlCommand(SqlCmd, myConnection); myConnection.Open(); try { myCommand.ExecuteNonQuery(); BindDataGrid(); } catch(SqlException ex) { lblMessage.Visible = true; lblMessage.Text = ex.Message; } } else { lblMessage.Visible = true; lblMessage.Text = "请选择记录,再进行删除操作!"; } }批量删除的话上面就可以,但是要好一点的话就再加一个checkbox在外面,叫它ckbAll,在CheckedChanged事件里写这个:private void ckbAll_CheckedChanged(object sender, System.EventArgs e) { foreach(DataGridItem thisItem in DataGrid1.Items) { ((CheckBox)thisItem.Cells[0].FindControl("ItemCheckBox")).Checked = ckbAll.Checked; } }这个是全选功能 在删除事件中string id="";for(int i=0;i<this.DataGrid1.Items.Count;i++){ if(this.DataGrid1.Items[i].ItemType==ListItemType.Item) { CheckBox cb=(CheckBox)this.DataGrid1.Items[i].FindControl("CheckBox1"); Label _lblId=(Label)this.DataGrid1.Items[i].FindControl("lblId"); if(cb.Checked) { id+=_lblId.Text.Trim(); } }}///这里根据id的值删除处理 随便问一句 Ewangsoft (河南§小王) 是郑州的吧! foreach (DataGridItem item in DataGrid1.Items){ CheckBox cbx=(CheckBox)item.FindControl("cbx_select");if(cbx.Checked){//相应处理。} } 在DataGrid中操作CheckBox控件http://nhlinkin.cnblogs.com/articles/113590.html 关于增加下拉列表项目的问题 关于sitemap生成menu,按role显示节点的问题 我想问下???紧急。 服务器应用程序不可用.......... 为什么?? 如何动态添加一个服务器端控件到Table的指定单元格 请教asp.net调用crystal report 的问题 求救! 在Reporting Service中如何根据报表参数是否为空自动调整查询条件 Crystal 是否可以用於 asp.net 的打印中, 有沒有演示地址 如何實現查詢功能與打印報表功能 asp.net连接Access数据库 怎样连接虚拟目录中的access数据库
if(chk.checked==true) //delete;
C#中注意大小写
{
if(((CheckBox)thisItem.Cells[0].FindControl("ItemCheckBox")).Checked)
{
arrID += DataGrid1.DataKeys[thisItem.ItemIndex].ToString() + ",";
}
}
if (arrID.Length > 1)
{
arrID = arrID.Substring(0,arrID.Length-1); // 数据库连接
SqlConnection myConnection = new SqlConnection(sconn);
string SqlCmd = "Delete From table Where ....";
SqlCommand myCommand = new SqlCommand(SqlCmd, myConnection);
myConnection.Open();
try
{
myCommand.ExecuteNonQuery();
BindDataGrid();
}
catch(SqlException ex)
{
lblMessage.Visible = true;
lblMessage.Text = ex.Message;
}
}
else
{
lblMessage.Visible = true;
lblMessage.Text = "请选择记录,再进行删除操作!";
}
}批量删除的话上面就可以,但是要好一点的话就再加一个checkbox在外面,叫它ckbAll,在CheckedChanged事件里写这个:private void ckbAll_CheckedChanged(object sender, System.EventArgs e)
{
foreach(DataGridItem thisItem in DataGrid1.Items)
{
((CheckBox)thisItem.Cells[0].FindControl("ItemCheckBox")).Checked = ckbAll.Checked;
}
}这个是全选功能
string id="";
for(int i=0;i<this.DataGrid1.Items.Count;i++)
{
if(this.DataGrid1.Items[i].ItemType==ListItemType.Item)
{
CheckBox cb=(CheckBox)this.DataGrid1.Items[i].FindControl("CheckBox1");
Label _lblId=(Label)this.DataGrid1.Items[i].FindControl("lblId");
if(cb.Checked)
{
id+=_lblId.Text.Trim();
}
}
}
///这里根据id的值删除处理
是郑州的吧!
{
CheckBox cbx=(CheckBox)item.FindControl("cbx_select");
if(cbx.Checked)
{
//相应处理。
}
}
http://nhlinkin.cnblogs.com/articles/113590.html