计算asp页面上的复选框数量 [英] Counting number of checkboxes on a asp page

查看:128
本文介绍了计算asp页面上的复选框数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码计算我的复选框列表控件中的所有选中项目。



I was using thge following code to count all checked items in my checkbox list control.

var totalcount = CheckBoxList1.Items.Cast<ListItem>().Where(item => item.Selected).Count()





但是现在我想使用单个复选框条目而不是复选框列表。如何将此代码更改为页面上的所有已选中复选框?



But now i would like to use single checkbox entries instead of a checkbox list. How could this code be changed to the count all checked checkboxes on a page?

推荐答案

在c#

int count = 0;

foreach(在this.form1.Controls中控制c)

{

if(c是CheckBox)

{

if(((CheckBox)c).Checked)

count = count + 1;

}

}

ClientScript.RegisterClientScriptBlock(GetType(),sas,javascript:alert(''+ count +'');,true);



在Jquery你可以这样做



var count =
In c#
int count=0;
foreach (Control c in this.form1.Controls)
{
if (c is CheckBox)
{
if (((CheckBox)c).Checked)
count = count + 1;
}
}
ClientScript.RegisterClientScriptBlock(GetType(), "sas", "javascript:alert(''"+count+"'');", true);

In Jquery U can do like this

var count =


(input [type =复选框] ]:选中)。长度;

提醒(计数);
("input[type=checkbox]:checked").length;
alert(count);


是的,如果它需要在面板下面它将起作用

like bellow

aspx

< asp:panel id =Panel1runat =serverxmlns:asp =#unknown>



< asp:checkbox id =CheckBox1checked =truerunat =server>

< asp:checkbox id =CheckBox2runat =server>

< asp:checkbox id =CheckBox3runat =server>

< asp:checkbox id =CheckBox4checked =truerunat =server>

< asp:checkbox id =CheckBox5runat =server>

< asp:checkbox id =CheckBox6runat =server>

< asp:checkbox id =CheckBox7runat =server>



aspx.cs

protected void Page_Load(object sender,EventArgs e)

{

int count = 0;

foreach(Panel1.Controls中的控制c)

{

if(c是CheckBox)

{

if(((CheckBox)c).Checked)

count = count + 1;

}

}



ClientScript.RegisterClientScriptBlock(GetType(),sas,javascript:ale rt(''+ count +'');,true);
yes it will work if it takes under panel
like bellow
aspx
<asp:panel id="Panel1" runat="server" xmlns:asp="#unknown">

<asp:checkbox id="CheckBox1" checked="true" runat="server">
<asp:checkbox id="CheckBox2" runat="server">
<asp:checkbox id="CheckBox3" runat="server">
<asp:checkbox id="CheckBox4" checked="true" runat="server">
<asp:checkbox id="CheckBox5" runat="server">
<asp:checkbox id="CheckBox6" runat="server">
<asp:checkbox id="CheckBox7" runat="server">

aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
int count = 0;
foreach (Control c in Panel1.Controls)
{
if (c is CheckBox)
{
if (((CheckBox)c).Checked)
count = count + 1;
}
}

ClientScript.RegisterClientScriptBlock(GetType(), "sas", "javascript:alert(''" + count + "'');", true);


这篇关于计算asp页面上的复选框数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆