遍历CheckBox

按钮CheckBox 全选反选
protected void Button1_Click(object sender, EventArgs e)
    {
        if (this.Button1.Text == "全选")
        {
            foreach (DataListItem dli in this.DataList1.Items)
            {
                CheckBox cb = (CheckBox)dli.FindControl("select");
                cb.Checked = true;
            }
            this.Button1.Text = "全选取消";
        }
        else
        {
            foreach (DataListItem dli in this.DataList1.Items)
            {
                CheckBox cb = (CheckBox)dli.FindControl("select");
                cb.Checked = false;
            }
            this.Button1.Text = "全选";
 
        }
    }
 
//datalist上的checkbox控件
        foreach (DataListItem var in this.DataList1.Items)
        {
            if (var is CheckBox)
            {
                char cb = (CheckBox)var;
            }
        }
     //   checkboxlist
        foreach (ListItem var in this.CheckBoxList1.Items)
        {
            if (var.Selected)
            {
                CheckBoxList cbl = (CheckBoxList)var;
            }
        }
遍历 checkbox
foreach (Control ct in form1.Controls)
        {
            if (ct.GetType().ToString().Equals("System.Web.UI.WebControls.CheckBox"))
            {

                CheckBox cb = (CheckBox)ct;
                cb.Checked = true ;

            }
        }
GridView遍历CheckBox
首先对CheckBox1也就是全选的那个CheckBox添加CheckBox1_CheckedChanged事件,CheckBox的AtuoPostBack要设置为True 自动提交到服务器属性
<asp:TemplateField>
                    <HeaderTemplate>
                        <asp:CheckBox ID="CheckBox1" runat="server"  Text="全选" TextAlign="left" OnCheckedChanged="CheckBox1_CheckedChanged" AutoPostBack="true"/></HeaderTemplate>
                   <ItemTemplate>
                       <asp:CheckBox ID="CheckBox2" runat="server"  /></ItemTemplate>
                    </asp:TemplateField>
 把下面事件复制到后台page_load页中即可
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        int i;
        if (((CheckBox)sender).Checked)
        {
            for (i = 0; i < GridView1.Rows.Count; i++)
            {
                ((CheckBox)GridView1.Rows[i].FindControl("CheckBox2")).Checked = true;
            }
        }
        else
        {
            for (i = 0; i < GridView1.Rows.Count; i++)
            {
                ((CheckBox)GridView1.Rows[i].FindControl("CheckBox2")).Checked = false;
            }
        }
    }
表格内放置checkbox遍历
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)//全选控件text="全选",
//控件名为checkbox1
    {
        if (this.CheckBox1.Checked)
        {


            foreach (Control ct in form1.Controls)
            {
                if (ct.GetType().ToString().Equals("System.Web.UI.WebControls.CheckBox"))
                {

                    CheckBox cb = (CheckBox)ct;
                    cb.Checked = true;

                }

            }
            this.CheckBox1.Text = "反选";
        }
        else
        {
            foreach (Control ct in form1.Controls)
            {
                if (ct.GetType().ToString().Equals("System.Web.UI.WebControls.CheckBox"))
                {

                    CheckBox cb = (CheckBox)ct;
                    cb.Checked = false;

                }

            }
            this.CheckBox1.Text = "全选";

        }
       
    }

 /// <summary>
    /// 遍历web窗体上的(System.Web.UI.WebControls.TextBox)控件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button2_Click(object sender, EventArgs e)
    {
        foreach (Control var in this.form1.Controls)
        {
            if(var.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox"))
            {
                TextBox cb = (TextBox)var;
                cb.Text="";
            }
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值