遍历CheckBox

来源:互联网 发布:浙江农家乐数据 编辑:程序博客网 时间:2024/05/16 18:15

按钮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="";
            }
        }
    }

原创粉丝点击