Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net DataGrid中的复选框_Asp.net_Vb.net_Checkbox_Datagrid - Fatal编程技术网

Asp.net DataGrid中的复选框

Asp.net DataGrid中的复选框,asp.net,vb.net,checkbox,datagrid,Asp.net,Vb.net,Checkbox,Datagrid,我有一个GridView,其中包含一个带有复选框的列。标题模板有一个复选框,用于选择列中的所有其他复选框 当我勾选并执行某个事件时,我收到以下错误: Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack tr

我有一个GridView,其中包含一个带有复选框的列。标题模板有一个复选框,用于选择列中的所有其他复选框

当我勾选并执行某个事件时,我收到以下错误:

Object reference not set to an instance of an object. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
在这一行:

ifchecked = DirectCast(item.FindControl("ckTransferItem"), CheckBox).Checked
数据网格代码:

<asp:DataGrid id="Dgcabinet" runat="server" CssClass="datagrids psLink" Width="100%" BorderStyle="None"
ForeColor="Transparent" GridLines="Horizontal" PagerStyle-Mode="NumericPages" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" PageSize="200">
    <ItemStyle CssClass="datagrids psLink"></ItemStyle>
    <HeaderStyle ForeColor="White" CssClass="datagridsHeader"></HeaderStyle>
    <Columns>
    <asp:BoundColumn Visible="False" DataField="file_pk"></asp:BoundColumn>
    <asp:BoundColumn DataField="file_pk" SortExpression="file_pk" HeaderText="&amp;nbsp;File Number">
    <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
    <ItemStyle HorizontalAlign="Center"></ItemStyle>
    </asp:BoundColumn>
    <asp:BoundColumn Visible="False" DataField="file_subject"></asp:BoundColumn>
    <asp:BoundColumn DataField="file_subject" SortExpression="file_subject" HeaderText="&amp;nbsp;File Name">
    <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
    <ItemStyle HorizontalAlign="Center"></ItemStyle>
    </asp:BoundColumn>
    <asp:BoundColumn Visible="False" DataField="cab_pk"></asp:BoundColumn>
    <asp:BoundColumn DataField="cab_pk" SortExpression="cab_pk" HeaderText="&amp;nbsp;Storage Number">
    <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
    <ItemStyle HorizontalAlign="Center"></ItemStyle>
    </asp:BoundColumn>
    <asp:BoundColumn Visible="False" DataField="comp_part"></asp:BoundColumn>
    <asp:BoundColumn DataField="comp_part" SortExpression="comp_part" HeaderText="&amp;nbsp;Compartment No.">
    <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
    <ItemStyle HorizontalAlign="Center"></ItemStyle>
    </asp:BoundColumn>
    <asp:BoundColumn Visible="False" DataField="dept_pk"></asp:BoundColumn>
    <asp:BoundColumn DataField="dept_code" SortExpression="dept_code" HeaderText="&amp;nbsp;Department Code">
    <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
    <ItemStyle HorizontalAlign="Center"></ItemStyle>
    </asp:BoundColumn>
    <asp:BoundColumn DataField="locationno" SortExpression="locationno" HeaderText="&amp;nbsp;Storage Location">
    <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
    <ItemStyle HorizontalAlign="Center"></ItemStyle>
    </asp:BoundColumn>
    <asp:TemplateColumn HeaderText="Tick to Transfer">
    <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
    <ItemStyle HorizontalAlign="Center"></ItemStyle>
    <ItemTemplate>
    <asp:CheckBox ID="ckTransferItem" Runat="server" AutoPostBack="false"></asp:CheckBox>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    <PagerStyle CssClass="datagridsFooter psLink" Mode="NumericPages"></PagerStyle>
</asp:DataGrid>
这是我尝试过的,但仍然得到相同的错误:

For Each dgItem As DataGridItem In Dgcabinet.Items

    If (dgItem.ItemType = ListItemType.Item) OrElse (dgItem.ItemType = ListItemType.AlternatingItem) Then
    
        ifchecked = DirectCast(dgItem.FindControl("ckTransferItem"), CheckBox).Checked
        department = Integer.Parse(dgItem.Cells(8).Text)

End If

Next
有人能解释为什么吗


如果选中该复选框,则应将其设置为True否?

在找到控件之前,只需检查
datagrid
项的
ItemType
。在大多数情况下,第一个项目或行总是
标题类型
,它会尝试在标题中找到该复选框,这就是它返回空值的原因

  If (item.ItemType ==  ListItemType.Item) || 
         (e.Item.ItemType == ListItemType.AlternatingItem) Then
        ifchecked = DirectCast(item.Cells[11].FindControl("ckTransferItem"), CheckBox).Checked
        //Your other code  
  End If 
阅读更多关于

还可以用foreach循环替换所有for循环代码

     foreach(DataGridItem item in Dgcabinet.Items)
     {
        if((item.ItemType ==  ListItemType.Item) || 
           (e.Item.ItemType == ListItemType.AlternatingItem))
          {
             ifchecked = DirectCast(item.Cells[11].FindControl("ckTransferItem"), CheckBox).Checked
             department = int.Parse(item.Cells(8).Text())
             If ifchecked Then
                fileIDs.Add((item.Cells(0).Text()))
                switch = True
             End If
          }
     }

请发布负责查找和更新复选框状态的vb.net代码。上面代码中的
项是什么?从上面的代码中,我可以看出它找不到控件并返回null。@CoderofCode Hi,抱歉。。我编辑了主要问题以包含按钮的事件处理程序代码
item
是一个datagrid项我如何才能做到这一点?当我选中一个复选框或选中所有复选框时,就会发生这种情况。我仍然会得到相同的错误。有关我尝试过的代码,请参阅更新的答案。请尝试更新。只需确保单元格[11],其中
11
是从0基索引开始的列编号包含复选框。问题是找不到复选框控件。我尝试了
Dim cb As CheckBox=item.FindControl(“ckTransferItem”)
但cb的值仍然为“Nothing”是的,我知道这就是为什么尝试
Dim cb As CheckBox=item.Cells[11]。FindControl(“ckTransferItem”)
仍然是相同的错误。我试图通过从HTML源中查找元素名来调用它(因为我认为它在运行应用程序时更改了名称),但它仍然找不到。也许这在使用数据网格时是不可能的?
     foreach(DataGridItem item in Dgcabinet.Items)
     {
        if((item.ItemType ==  ListItemType.Item) || 
           (e.Item.ItemType == ListItemType.AlternatingItem))
          {
             ifchecked = DirectCast(item.Cells[11].FindControl("ckTransferItem"), CheckBox).Checked
             department = int.Parse(item.Cells(8).Text())
             If ifchecked Then
                fileIDs.Add((item.Cells(0).Text()))
                switch = True
             End If
          }
     }