GridView有一列是CheckBox,怎么获取CheckBox的Checked为true的CheckBox.Text
我先做个批量删除GridView中CheckBox的Checked为true的记录.
数据的ID存在CheckBox.Text里.
怎样才能获取所有CheckBox的Checked为true的CheckBox.Text?
------解决方案--------------------HTML代码页面:添加
<input type= "checkbox " id= "rechkbox " runat= "server " value= ' <%#DataBinder.Eval(Container.DataItem, "Id ") %> ' />
CS代码页面的,取所有选中的checkbox
foreach (GridViewRow i in this.gridView.Rows)
{
HtmlInputCheckBox chkbox = i.FindControl( "rechkbox ") as HtmlInputCheckBox;
if (chkbox != null && chkbox.Checked)
{
int reId = Convert.ToInt32(chkbox.Value); //取checkbox的value
//其他的操作
}
}
------解决方案--------------------我不是用findcontrol
假如你的checkbox在gridview的第1列:
for(int i=0;i <= this.gridview1.Rows.Count;i++)
{
if(((CheckBox)gridview1.Rows[i].Cells[0].Controls[1]).Checked == false)
{
//操作本行
}
}
以上代码手打....可能个别字母写错,关键在于(CheckBox)gridview1.Rows[i].Cells[0].Controls[1] 就可以获得那个checkbox 如果你的checkbox不在第一列,修改Cells[0]即可,从0开始的索引.
------解决方案--------------------Private Sub Delectbutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Delectbutton.Click
Dim DataDelete As New modern
Dim i As Integer
For i = 0 To DataGrid1.Items.Count - 1
Dim cb As CheckBox = DataGrid1.Items(i).FindControl( "chkExport ")
If cb.Checked Then
Dim LabelId As Label = DataGrid1.Items(i).FindControl( "label1 ")
Dim CheckId As Integer
CheckId = Convert.ToInt32(LabelId.Text)
DataDelete.DataDelete(CheckId)
End If
Next
CreateDataSource()
End Sub