在DetailsView中有一个CheckBoxList控件,我在DetailsView的DataBound事件中取CheckBoxList的值,用
CheckBoxList CheckBoxList1 = (CheckBoxList)DetailsView1.FindControl("CheckBoxList1");
总是提示错误:
“未将对象引用设置到对象的实例。 ”请教各位如何解决?代码:
            <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" CellPadding="4"
                DataSourceID="ObjectDataSource1" ForeColor="#333333" GridLines="None" Height="50px"
                Width="335px" DataKeyNames="UserName" OnDataBound="DetailsView1_DataBound">
                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
                <EditRowStyle BackColor="#999999" />
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                <Fields>
                    <asp:BoundField DataField="UserName" HeaderText="UserName" ReadOnly="True" SortExpression="UserName" Visible="False" />
                    <asp:TemplateField HeaderText="名称:" SortExpression="types">
                        <EditItemTemplate>
                            <asp:CheckBoxList ID="CheckBoxList1" runat="server">
                                      <asp:ListItem Value="1">11</asp:ListItem>
                                      <asp:ListItem Value="2">22</asp:ListItem>
                                      <asp:ListItem Value="3">33</asp:ListItem>
                                      <asp:ListItem Value="4">44</asp:ListItem>
                                      <asp:ListItem Value="5">55</asp:ListItem>
                                      <asp:ListItem Value="6">66</asp:ListItem>
                            </asp:CheckBoxList>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Bind("types") %'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                 </Fields>
                <FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" />
                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            </asp:DetailsView>

解决方案 »

  1.   

    <EditItemTemplate> 你把子控件放在编辑状态下了,DetailsView的DataBound事件里应该只能找到<ItemTemplate> 里面的控件
    你试试找 <asp:Label ID="Label1" runat="server" Text=' <%# Bind("types") %'> </asp:Label>看看,如果找得到,就应该是这个问题了.^O^
      

  2.   


    试了,可以取到Label1的值,但如何取到 EditItemTemplate 的控件呢??
      

  3.   

    要看当前DetailsView的状态.如果在Edit mode 下用DetailsView.FindControl("Id")可以找到edititemtemplate里的,如果在readonly mode下找到的是ItemTemplate里的.
      

  4.   


    支持楼上,原来在readonly mode下没有CheckBoxList1,所以出错。明白,谢谢!!