当前位置:编程学习 > C#/ASP.NET >>

checkbox全选,怎么提示未将对象引用设置到对象的实例

  if (Button2.Text == "全选")
        {
            for (int i = 0; i < table1.Rows.Count;i++)
            {
                CheckBox cb = (CheckBox)table1.Rows[i].Cells[0].FindControl("checkbox");
                cb.Checked = true;
                Button2.Text = "全不选";
            }
            return;
        }
        if (Button2.Text == "全不选")
        {
            for (int i = 0; i < table1.Rows.Count;i++)
            {
                CheckBox cb = (CheckBox)table1.Rows[i].Cells[0].FindControl("checkbox");
                cb.Checked = false;
                Button2.Text = "全选";
               
            }
            return;
        }


传入的cb值都未null,提示未将对象引用设置到对象的实例 --------------------编程问答-------------------- 未找到控件,你那个事件是在哪个控件里写的啊 --------------------编程问答-------------------- 按钮单击事件  protected void Button2_Click(object sender, EventArgs e)
    {
        
        if (Button2.Text == "全选")
        {
            for (int i = 0; i < table1.Rows.Count;i++)
            {
                CheckBox cb = (CheckBox)table1.Rows[i].Cells[0].FindControl("checkbox");
                cb.Checked = true;
                Button2.Text = "全不选";
            }
            return;
        }
        if (Button2.Text == "全不选")
        {
            for (int i = 0; i < table1.Rows.Count;i++)
            {
                CheckBox cb = (CheckBox)table1.Rows[i].Cells[0].FindControl("checkbox");
                cb.Checked = false;
                Button2.Text = "全选";
               
            }
            return;
        }
    } --------------------编程问答-------------------- table1.Rows[i].Cells[0].FindControl("checkbox");
这个肯定是在一个控件里的把 --------------------编程问答-------------------- 额,我是从一个是gridview的页面考过来的代码。
我这个checkbox都是有名字的,可能就获取不到这个数据了


那要怎么全部选中checkbox,在table里面 --------------------编程问答-------------------- CheckBox cb = (CheckBox)table1.Rows[i].FindControl("你的CheckBox的ID值");
--------------------编程问答-------------------- 这种功能为什么不在客户端实现呢 弄到服务器端实现 实在是太浪费了 --------------------编程问答-------------------- 干嘛要提交后台呢?用js写个就行了,提交后台不怕占用性能啊??? --------------------编程问答--------------------

 1. GridView中的代码主要片段:

        <Columns>

            <HeaderTemplate>       //头模板代码
                   <asp:CheckBox id="chkHeader" runat="server" AutoPostBack="False"   //AutoPostBack设为假,不需要回发

                            onclick="javascript:SelectAll(this);"></asp:CheckBox>
            </HeaderTemplate>

           <ItemTemplate>            //项模板代码
                   <asp:CheckBox id="chkItem" runat="server"></asp:CheckBox>
           </ItemTemplate>

        </Columns>

2.  在当页加入:

  <script language="javascript">
       function SelectAll(tempControl)
       {
           //将除头模板中的其它所有的CheckBox取反 

            var theBox=tempControl;
             xState=theBox.checked;    

            elem=theBox.form.elements;
            for(i=0;i<elem.length;i++)
            if(elem[i].type=="checkbox" && elem[i].id!=theBox.id)
             {
                  if(elem[i].checked!=xState)
                        elem[i].click();
            }
  }  
</script>


--------------------编程问答-------------------- 新手JS不会用,汗 --------------------编程问答--------------------
引用 5 楼 dalmeeme 的回复:
CheckBox cb = (CheckBox)table1.Rows[i].FindControl("你的CheckBox的ID值");



这个不是只能选中一个吗,不能全部全中,我有几十个checkbox --------------------编程问答-------------------- 看来我可以给自己加分了,刚才借鉴别人的做出来了
   protected void Button1_Click(object sender, EventArgs e)
    {
        CheckBox chkTemp;
        if (Button1.Text == "全选")
        {
        foreach (Control c in this.form1.Controls)
        {
            chkTemp = c as CheckBox;
            if (chkTemp != null)
            chkTemp.Checked = true;
            Button1.Text = "全不选";
         }
        return;
       }
        if (Button1.Text == "全不选")
        {
            foreach (Control c in this.form1.Controls)
            {
                chkTemp = c as CheckBox;
                if (chkTemp != null)
                    chkTemp.Checked = false;
                Button1.Text = "全选";

            }
            return;
        }
        } --------------------编程问答-------------------- protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
    {//复选
        if (CheckBox2.Checked == true)
        {
            CheckBox2.Text = "全选";
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");

                cbox.Checked = true;
            }
        }
        else
        {
            CheckBox2.Text = "全不选";
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");

                cbox.Checked = false;
            }
        }
    }
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,