我在模板字段中有CheckBoxList:
<asp:TemplateField HeaderText="Check Box">
<ItemTemplate>
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
<asp:ListItem></asp:ListItem>
</asp:CheckBoxList>
</ItemTemplate>
</asp:TemplateField> 我想检查是否已选中所有复选框。如果未选中所有复选框,则无法继续。
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow row = GridView1.Rows[i];
bool isChecked = ((CheckBoxList)row.FindControl("CheckBoxList1")).Checked;
if (isChecked)
Response.Write("Its Checked");
else
Response.Write("Not Check");
}问题是它总是返回“is”,即使它不是。可能是因为我不能在模板视图中使用CheckBoxList。并且复选显然不是"CheckBoxList“方法的属性
发布于 2013-01-24 01:40:19
您需要使用CheckBoxList.SelectedItems.Count将其与总项目进行比较,以确定是否选择了所有项目。
CheckBoxList CheckBoxList1 = ((CheckBoxList)row.FindControl("CheckBoxList1")).Enabled;
int i = 0;
for(i = 0; i < CheckBoxList1.Items.Count; i++)
if (!CheckBoxList1.Items[i].Checked)
break;
if(i == CheckBoxList1.Items.Count)
Response.Write("Its Checked");
else
Response.Write("Not Check"); 发布于 2013-01-24 01:47:23
你应该仔细检查所有的项目,并计算其中有多少是选中的。,然后将该值与checkboxlist中的项目总数进行比较。
for (int i = 0; i < GridView1.Rows.Count; i++) {
GridViewRow row = GridView1.Rows[i];
if(row.RowType == DataControlRowType.DataRow) {
CheckBoxList CheckBoxList1= row.FindControl("CheckBoxList1")) as CheckBoxList;
//CheckBoxList CheckBoxList1= row.Cells[cbCellIndex].FindControl("CheckBoxList1")) as CheckBoxList;
int checkedCount = 0;
foreach (ListItem item in CheckBoxList1.Items) {
checkedCount += item.Selected ? 1 : 0;
}
if (checkedCount == CheckBoxList1.Items.Count) {
//all checked
}
else if (checkedCount == 0)
{
//none checked
}
}
}}和Enabled仅显示用户是否可以与其交互。如果为Enabled == false,您将看到已禁用的复选框
https://stackoverflow.com/questions/14485874
复制相似问题