GridView1我正试图在gridview上切换我的代码:
<asp:SqlDataSource ID="dsPar" runat="server" ConnectionString="<%$ ConnectionStrings:connstring %>"
SelectCommand="SELECT ID, FileNumber, address, phone from myTable ORDER BY ID" FilterExpression="ID like '%{0}%'">
<FilterParameters>
<asp:ControlParameter Name="StreetSrch" ControlID="searchBox" PropertyName="Text" />
</FilterParameters>
</asp:SqlDataSource>
到此:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim strQuery As String = "SELECT ID, FileNumber, address, Phone from MyTable WHERE Id LIKE '%@strSearch%' ORDER BY Id"
Dim cmd As New SqlCommand(strQuery)
Dim dt As DataTable = GetData(cmd)
Dim CheckBoxArray As ArrayList
If ViewState("CheckBoxArray") IsNot Nothing Then
CheckBoxArray = DirectCast(ViewState("CheckBoxArray"), ArrayList)
Else
CheckBoxArray = New ArrayList()
End If
If Not IsPostBack Then
Gridview1.DataBind()
Dim CheckBoxIndex As Integer
Dim CheckAllWasChecked As Boolean = False
Dim chkAll As CheckBox = DirectCast(GridView1.HeaderRow.Cells(0).FindControl("chkAll"), CheckBox)
rest of code....
End If
End Sub
最后,下面是标记的快照:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowPaging="True"
OnPageIndexChanging = "OnPaging" HeaderStyle-CssClass = "header" Font-Size = "10pt"
AlternatingRowStyle-BackColor = "#C2D69B" OnRowDataBound = "RowDataBound" AllowSorting="true"
PageSize="20" CssClass="Gridview"
GridLines="None">
我在下一行收到"Object reference not set to an instance of an object."
:
第44行:Dim chkAll As CheckBox = DirectCast(GridView1.HeaderRow.Cells(0).FindControl(“chkAll”),CheckBox)
知道如何解决这个问题吗?
代码很长,我不想在这里发布所有内容。如果需要,我可以发布更多内容。
答案 0 :(得分:0)
尝试调暗作为对象的新实例,这可能会导致您的问题?当我得到这个特殊的例外情况时,通常是因为我在没有'新'
的情况下黯然失色希望这有帮助!
答案 1 :(得分:0)
在该行上打破调试器。然后,您可以使用“快速监视”功能查看行的各个部分,以查看什么是null。首先看一下GridView1。如果那不是null,请查看GridView1.HeaderRow。继续,直到找到空对象。
答案 2 :(得分:0)
您可以使用断点和监视窗口来查找代码出错的位置。每当您尝试在NullReferenceException
的变量上调用方法时,都会抛出Nothing
。
选项推断的代码示例:
Dim firstHeaderCell = GridView1.HeaderRow.Cells(0)
Debug.Assert(firstHeaderCell IsNot Nothing, "Couldn't find the first header cell")
Dim chkAllControl = firstHeaderCell.FindControl("chkAll")
Debug.Assert(chkAllControl IsNot Nothing, "Couldn't find a control named chkAll")
Dim chkAll As CheckBox = TryCast(chkAllControl, CheckBox)
Debug.Assert(chkAll IsNot Nothing, "chkAll exists but it isn't a CheckBox")
这些断言完全是矫枉过正,但是不能给出带有“断点”和“监视窗口”变量的代码示例。
答案 3 :(得分:0)
IT正在抛出空引用异常,因为
GridView1.HeaderRow.Cells(0).FindControl("chkAll")
没有找到一个复选框,所以它返回null(Nothing)所以你实际上正在尝试将Nothing转换为复选框。