出于某种原因,我得到的对象引用未设置为对象的实例。
Protected Sub GridView6_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
Dim date1 As Date
date1 = Date.Now
Dim date2 As Date
Dim ddate As Label = CType(e.Row.FindControl("label1"), Label)
date2 = Date.Parse(ddate.Text)
Dim ts As TimeSpan = date2.Subtract(date1)
Dim days As Integer = ts.TotalDays
If days <= 14 Then
e.Row.ForeColor = System.Drawing.Color.Red
ElseIf days > 14 And ts.Days < 30 Then
e.Row.ForeColor = System.Drawing.Color.Blue
ElseIf days >= 30 Then
e.Row.ForeColor = System.Drawing.Color.LightGreen
End If
End Sub
对象引用未设置为 对象的实例。描述: 在此期间发生了未处理的异常 当前网络的执行 请求。请查看堆栈跟踪 有关错误的更多信息 它起源于代码。
异常详细信息: System.NullReferenceException:Object 引用未设置为的实例 对象
来源错误:
Line 166: Line 167: Dim ddate As Label = CType(e.Row.FindControl("label1"), Label) Line 168: date2 = Date.Parse(ddate.Text) Line 169: Dim ts As TimeSpan = date2.Subtract(date1) Line 170: Dim days As Integer = ts.TotalDays
源文件:C:\ Documents and 设置\ ChrisH \桌面\ AJAXEnabledWebSite18 \ Default.aspx.vb 行:168
堆栈追踪:
[NullReferenceException:Object 引用未设置为的实例 宾语。] _Default.GridView6_RowDataBound(Object sender,GridViewRowEventArgs e)in C:\ Documents and 设置\ ChrisH \桌面\ AJAXEnabledWebSite18 \ Default.aspx.vb:168 System.Web.UI.WebControls.GridView.OnRowDataBound(GridViewRowEventArgs e)+108 System.Web.UI.WebControls.GridView.CreateRow(的Int32 rowIndex,Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState,Boolean dataBind,Object dataItem, DataControlField []字段, TableRowCollection行, PagedDataSource pagedDataSource)+167 System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable的 dataSource,Boolean dataBinding)+1651 System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable的 数据)+57 System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable的 数据)+14 System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable的 数据)+114 System.Web.UI.DataSourceView.Select(DataSourceSelectArguments 参数, DataSourceViewSelectCallback回调) +31 System.Web.UI.WebControls.DataBoundControl.PerformSelect() +142 System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +73 System.Web.UI.WebControls.GridView.DataBind() +4 System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +82 System.Web.UI.WebControls.BaseDataBoundControl.OnPreRender(EventArgs e)+22 System.Web.UI.WebControls.GridView.OnPreRender(EventArgs的 e)+17 System.Web.UI.Control.PreRenderRecursiveInternal() +80 System.Web.UI.Control.PreRenderRecursiveInternal() +171 System.Web.UI.Control.PreRenderRecursiveInternal() +171 System.Web.UI.Control.PreRenderRecursiveInternal() +171 System.Web.UI.Control.PreRenderRecursiveInternal() +171 System.Web.UI.Page.ProcessRequestMain(布尔值 includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)+842
答案 0 :(得分:3)
ddate
为null
,因为没有label1
。
答案 1 :(得分:3)
据说行中没有名为“label1”的控件。这很可能是正确的,因为该行只能有Cells
。您需要在基于零的索引上按列访问特定单元格:
Dim ddate As Label = CType(e.Row.Cells(2).FindControl("label1"), Label)
请注意更改上方(2)
以匹配列号。
编辑:您似乎没有进行任何检查以确保您正在查看DataRow与HeaderRow或FooterRow。这是一个片段:
Protected Sub GridView6_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
' CHECK THE ROW TYPE HERE. ONLY EXECUTE ON DataRow
If e.Row.RowType = DataControlRowType.DataRow Then
Dim date1 As Date
date1 = Date.Now
Dim date2 As Date
Dim ddate As Label = CType(e.Row.FindControl("label1"), Label)
date2 = Date.Parse(ddate.Text)
Dim ts As TimeSpan = date2.Subtract(date1)
Dim days As Integer = ts.TotalDays
If days <= 14 Then
e.Row.ForeColor = System.Drawing.Color.Red
ElseIf days > 14 And ts.Days < 30 Then
e.Row.ForeColor = System.Drawing.Color.Blue
ElseIf days >= 30 Then
e.Row.ForeColor = System.Drawing.Color.LightGreen
End If
End If
End Sub