我有一个包含带复选框的列的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
DataGrid代码:
<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="&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="&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="&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="&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="&nbsp;Department Code">
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="locationno" SortExpression="locationno" HeaderText="&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>
按钮事件处理程序:
Private Sub btnbulktransfer_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理btnbulktransfer.Click '获取datagrid中的行数 Dim rowcount As Integer = Dgcabinet.Items.Count Dim itemrow As Integer = 0 Dim item As DataGridItem Dim ifchecked As Boolean = False Dim getcompartment As Integer Dim getnewstate As Integer Dim getlocation As Integer Dim statetocomment As String Dim lbresult As Boolean = False Dim liUserRoleId As Integer Dim liUserId As Integer Dim switch As Boolean = False Dim linewdepartment As Integer Dim licurrentdepartment As Integer Dim lioldstates As Integer Dim listateid As Integer Dim lsImriehelRepository As String lioldstates = ddloldstates.SelectedValue
licurrentdepartment = ddldepts.SelectedValue
liUserRoleId = Convert.ToInt64(lcPrimitives.GetSessionCache("_USERROLE_ID"))
liUserId = Convert.ToInt64(lcPrimitives.GetSessionCache("_USER_ID"))
getlocation = ddllocation.SelectedValue
getcompartment = ddlstorage.SelectedValue
getnewstate = ddlnewstates.SelectedValue
statetocomment = ddlnewstates.Items(ddlnewstates.SelectedIndex).Text
linewdepartment = BLeRecords.getFileDepartment(getcompartment)
listateid = ddldepts.SelectedValue
'check if storage is in the imriehel repository
lsImriehelRepository = [Global]._eRecordsPortalConstants.eRecordsCONST_ToImriehelRepository
If linewdepartment = lsImriehelRepository Then
linewdepartment = licurrentdepartment
End If
'check that all 'transfer to' dropdown options are selected
If (getlocation = 0) Then
lblError.Visible = True
lblError.Text = "!Error. Please check location dropdown."
ElseIf (getcompartment = 0) Then
lblError.Visible = True
lblError.Text = "!Error. Please check Storage dropdown."
ElseIf (getnewstate = 0) Then
lblError.Visible = True
lblError.Text = "!Error. Please check Status dropdown."
Else
Dim fileIDs As ArrayList = New ArrayList
Dim department As Integer
ClearMessages()
For itemrow = 0 To rowcount - 1
Dim cb As CheckBox = item.Cells(11).FindControl("ckTransferItem")
If cb.Checked Then
ifchecked = True
Else
ifchecked = False
End If
department = Integer.Parse(item.Cells(8).Text())
If ifchecked Then
Dim filenumber As String = (item.Cells(0).Text())
fileIDs.Add(filenumber)
switch = True
End If
Next
'check if tickbox are ticked. If not returns error.
If switch = False Then
lblError.Visible = True
lblError.Text = "!Error. Please check tick box"
Else
lbresult = BLeRecords.updateBulkTransferBatch(fileIDs, getcompartment, linewdepartment, getnewstate, statetocomment, liUserId, listateid)
If lbresult = True Then
lblmessage.Text = "File transfer succesful"
lblmessage.Visible = True
DisplaySortedData("", lioldstates, licurrentdepartment, False)
Else
lblError.Visible = True
lblError.Text = "!Error. File transfer Failed. Contact Administrator"
End If
End If
End If
End Sub
这是我尝试的但我仍然得到同样的错误:
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 no?
答案 0 :(得分:0)
在找到控件之前,只需检查ItemType
项datagrid
即可。在大多数可能的情况下,第一个项目或行始终是headertype
,它会尝试在标题中找到该复选框,这就是它返回空值的原因。
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
详细了解ItemType here。
您还可以使用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
}
}