GivdView中CheckBox全选问题

时间:2022-03-27 10:28:42
如题,页面有一个id为allChooseCB的全选CheckBox,想实现当勾选后,GirdView1中一列CheckBox全部选中,但是出现了未将对象引用设置到对象的实例,代码如下,请高手看一下,问题出在哪,如何解决?
注:页面使用了母版页adminMain.master 控件放在可编辑模块concent中

前台源码

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
                AutoGenerateColumns="False" DataKeyNames="id,id1" 
                DataSourceID="concentSDS" CellPadding="4" ForeColor="#333333" 
                GridLines="None" Width="100%">
                <AlternatingRowStyle BackColor="White" />
                <Columns>
                    <asp:TemplateField HeaderText="选择">
                        <ItemTemplate>
                                <input id="rowCb" type="checkbox" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="id" HeaderText="索引" InsertVisible="False" 
                        ReadOnly="True" SortExpression="id" ItemStyle-Width="5%">
        <ItemStyle Width="5%"></ItemStyle>
                    </asp:BoundField>
                    <asp:BoundField DataField="classifyName" HeaderText="所属类别" 
                        SortExpression="classifyName" ItemStyle-Width="15%" >
        <ItemStyle Width="15%"></ItemStyle>
                    </asp:BoundField>
                    <asp:BoundField DataField="newsTitle" HeaderText="文章标题" 
                        SortExpression="newsTitle" ItemStyle-Width="50%">
        <ItemStyle Width="50%"></ItemStyle>
                    </asp:BoundField>
                    <asp:BoundField DataField="writer" HeaderText="编辑者" 
                        SortExpression="writer" ItemStyle-Width="10%">
        <ItemStyle Width="10%"></ItemStyle>
                    </asp:BoundField>
                    <asp:TemplateField HeaderText="修改">
                        <ItemTemplate>
                            <asp:LinkButton ID="LinkButton1" runat="server">修改</asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="删除">
                        <ItemTemplate>
                            <asp:LinkButton ID="LinkButton2" runat="server">删除</asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <EditRowStyle BackColor="#2461BF" />
                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                <RowStyle BackColor="#EFF3FB" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                <SortedAscendingCellStyle BackColor="#F5F7FB" />
                <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                <SortedDescendingCellStyle BackColor="#E9EBEF" />
                <SortedDescendingHeaderStyle BackColor="#4870BE" />
            </asp:GridView>
            <asp:SqlDataSource ID="concentSDS" runat="server" 
                ConnectionString="<%$ ConnectionStrings:ConnectionString %>">
               </asp:SqlDataSource>

<asp:CheckBox ID="allChooseCB" runat="server" text="全选" 
                        oncheckedchanged="allChooseCB_CheckedChanged" AutoPostBack="true"/>


后台checkBox代码:

    protected void allChooseCB_CheckedChanged(object sender, EventArgs e)
    {
        bool isChecked =((CheckBox)this.Page.Master.FindControl("concent").FindControl("allChooseCB")).Checked;
        foreach (GridViewRow gvRow in GridView1.Rows)
        {
((CheckBox)(gvRow.Cells[0].Page.Master.FindControl("concent").FindControl("rowCb"))).Checked = isChecked;
        }
    }


问题: ((CheckBox)(gvRow.Cells[0].Page.Master.FindControl("concent").FindControl("rowCb"))).Checked = isChecked;
出现了未将对象引用设置到对象的实例,貌似搜索不到GridView中的checkBox控件
,急死了,困扰了一个早上

6 个解决方案

#1


不用控件很多年

#2


判断下类型。
 if (gvRow.RowType == DataControlRowType.DataRow)
        { 
            //your code .
        }

#3


这个用jquery or javascript就很好实现

#4


用html控件吧
<asp:TemplateField HeaderText="全选">
    <HeaderTemplate>
      <input type="checkbox" name="checkAll" ID="cbAll" onclick="GetAllCheck(this)" runat="server" />全选
    </HeaderTemplate>
   <ItemTemplate>
     <input type="checkbox" name="checkBook" ID="cbBooks" runat="server" />
   </ItemTemplate>
   <ItemStyle Width="80px" />
</asp:TemplateField>

用js写全选
<script type="text/javascript">
   function GetAllCheck(checkAll) {
    var items = document.getElementsByTagName("input");
    for (var i = 0; i < items.length; i++) {
        items[i].checked = checkAll.checked;
    }
   }
</script>

#5


引用 4 楼 LYFFY 的回复:
用html控件吧
<asp:TemplateField HeaderText="全选">
    <HeaderTemplate>
      <input type="checkbox" name="checkAll" ID="cbAll" onclick="GetAllCheck(this)" runat="server" />全选
    </HeaderT……

++
用js在客户端处理吧,没必要回发的。

#6


GivdView中CheckBox全选问题 这么一大坨控件代码。看着就头疼。

#1


不用控件很多年

#2


判断下类型。
 if (gvRow.RowType == DataControlRowType.DataRow)
        { 
            //your code .
        }

#3


这个用jquery or javascript就很好实现

#4


用html控件吧
<asp:TemplateField HeaderText="全选">
    <HeaderTemplate>
      <input type="checkbox" name="checkAll" ID="cbAll" onclick="GetAllCheck(this)" runat="server" />全选
    </HeaderTemplate>
   <ItemTemplate>
     <input type="checkbox" name="checkBook" ID="cbBooks" runat="server" />
   </ItemTemplate>
   <ItemStyle Width="80px" />
</asp:TemplateField>

用js写全选
<script type="text/javascript">
   function GetAllCheck(checkAll) {
    var items = document.getElementsByTagName("input");
    for (var i = 0; i < items.length; i++) {
        items[i].checked = checkAll.checked;
    }
   }
</script>

#5


引用 4 楼 LYFFY 的回复:
用html控件吧
<asp:TemplateField HeaderText="全选">
    <HeaderTemplate>
      <input type="checkbox" name="checkAll" ID="cbAll" onclick="GetAllCheck(this)" runat="server" />全选
    </HeaderT……

++
用js在客户端处理吧,没必要回发的。

#6


GivdView中CheckBox全选问题 这么一大坨控件代码。看着就头疼。