C# checkboxlistreturnchecked
我在模板字段中有复选框列表:C# checkboxlistreturnchecked,c#,C#,我在模板字段中有复选框列表: <asp:TemplateField HeaderText="Check Box"> <ItemTemplate> <asp:CheckBoxList ID="CheckBoxList1" runat="server"> <asp:ListItem></asp:ListItem> </asp:CheckBo
<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");
}
for(int i=0;i
问题是它总是返回“its Checked”,即使它不是。可能是因为我无法在模板视图中使用复选框列表。而且Checked显然不是“CheckBoxList”方法的属性。您需要使用CheckBoxList.SelectedItems.Count将其和total items进行比较,以确定是否选中了所有项
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");
CheckBoxList CheckBoxList1=((CheckBoxList)row.FindControl(“CheckBoxList1”)。已启用;
int i=0;
对于(i=0;i
您应该检查所有项目,并计算检查的项目数量。然后将该值与复选框列表中的项目总数进行比较
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
}
}
}
for(int i=0;i
}
而Enabled只是显示用户是否可以与之交互。如果
Enabled==false
您将看到禁用的复选框提示:Enabled和checked不是同一件事lol我的第一篇文章,我已经被拒绝了?!难对付的人群。System.Web.UI.WebControls.CheckBoxList
没有名为SelectedItems
的属性。您可能想到的是System.Windows.Forms.CheckedListBox
。在asp.net Checkboxlist中没有SelectedItems PropertyAngel,这似乎是可行的,但是因为我的Checkboxlist位于我的GridView/TemplateField/ItemTemplate中,所以CheckBoxList1在代码背后不符合条件。据我所知,FindControl存在问题。请参阅更新的答案。记住GridView。行包含网格中的所有行。如页脚、页眉等。所以您需要检查行类型。假设DataRow是您所需要的。另外,我不确定find控件是否会发现您的复选框列表被应用于行
,因为您有一个注释行作为替代。只需计算列的数量,并将其代替cbCellIndex