在一些 ASP.Net 代码隐藏中,我调用了SelectRow
继承自System.Web.UI.WebControls.GridView
.
拨打电话:
If (ProgressGrid.Rows.Count > 0) Then
ProgressGrid.SelectRow(0)
End If
正如预期的那样,这会生成一个SelectedIndexChanged
事件,由处理程序拾取:
Protected Sub ProgressGrid_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ProgressGrid.SelectedIndexChanged
Using db As New DataContext
Dim course = (From c In db.CourseResults
Where c.MemberID = MemberID AndAlso c.ResultID = CInt(ProgressGrid.SelectedDataKey.Value)
Select c).Single
' more code here
End Using
End Sub
我的问题是ProgressGrid.SelectedDataKey
在nothing
我的事件处理程序内部,导致空引用错误。在使用 Visual Studio 2010 进行调试时,我可以从调用堆栈中看到ProgressGrid.SelectRow(0)
被命中并且ProgressGrid.Rows.Count
大于零。那么为什么对象上的所有“Selected...”属性都ProgressGrid
设置为nothing
or -1
?知道我做错了什么吗?
自定义 Grid 类包含覆盖默认GridView
行为的此属性:
Public Overrides Property SelectedIndex() As Integer
Get
If AutoPostback Or AllowSelect = False Then
Return MyBase.SelectedIndex
Else
If HttpContext.Current Is Nothing Then
Return Nothing
Exit Property 'Exit if in design mode
End If
Dim index As String = Page.Request(Me.ClientID + "_SelectedRow")
If (String.IsNullOrEmpty(index)) Then
If (ViewState("SelectedIndex") Is Nothing) Then
Return -1
Else
Return ViewState("SelectedIndex")
End If
Else
ViewState.Add("SelectedIndex", index)
Return CType(index, Integer)
End If
End If
End Get
Set(ByVal value As Integer)
MyBase.SelectedIndex = value
End Set
End Property
调试器无法显示详细信息,MyBase
第一次调用MyBase.SelectedIndex = value
调试器的快速监视窗口返回空引用错误。一旦我到达事件处理程序,上述属性中的断点表明它MyBase.SelectedIndex
是nothing
,尽管试图将其设置为零。