简体   繁体   English

如果选中了所有CheckBoxList项,如何显示或隐藏按钮?

[英]How can I show or hide a button if all CheckBoxList Items are checked?

i have this code .I want to make this like , when i checked all items , a button shows up . 我有此代码。我想使之类似,当我检查所有项目时,将显示一个按钮。

        string connStr = "myconnstring" ;
        SqlCommand com;
        SqlConnection con = new SqlConnection(connStr);
        string s1 = string.Empty;
        foreach (ListItem item in this.CheckBoxList1.Items)
        {
            if (item.Selected)
            {
                s1 = item.ToString();
                com = new SqlCommand("Insert into tblml values('" + s1 + "')", con);
                con.Open();
                com.ExecuteNonQuery();
                con.Close();
            }
        }
        Response.Write("Inserted Successfully");

Any help appriciate. 任何帮助。

Thanks 谢谢

You could determine if they are all checked like this: 您可以确定是否都像这样检查了它们:

var isAllChecked = this.CheckBoxList1.Items.OfType<ListItem>().All(l => l.Selected);

and then you could hide/show your button using that variable: 然后您可以使用该变量隐藏/显示按钮:

this.button.Visible = isAllChecked;

Another option would be to create a variable at the top of the loop you already have: 另一种选择是在循环的顶部创建一个变量:

var isAllChecked = true;

and then inside the loop add an else to the if (item.Selected) : 然后在循环if (item.Selected)添加else

    ...
}
else { isAllChecked = false; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM