Hopefully some one can help... I have been trying now for the past few days to find a button located in the footer of a gridview which has an empty datasource. I have used the following code to show the header and footer on the empty grid, but when i try to reference the button to set its visibility to false I get the error "Object reference not set to an instance of an object.".
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web.UI
Imports System.Web.UI.WebControls
Namespace xGridView
Public Class xGridView
Inherits GridView
#Region "Properties"
<Category("Behaviour")> _
<Themeable(True)> _
<Bindable(BindableSupport.No)> _
<DefaultValue(False)> _
Public Property ShowWhenEmpty() As Boolean
Get
If ViewState("ShowWhenEmpty") Is Nothing Then
Return False
End If
Return CBool(ViewState("ShowWhenEmpty"))
End Get
Set(ByVal value As Boolean)
ViewState("ShowWhenEmpty") = value
End Set
End Property
Protected _footerRow2 As GridViewRow
Public Overloads Overrides ReadOnly Property FooterRow() As GridViewRow
Get
Dim f As GridViewRow = MyBase.FooterRow
If f IsNot Nothing Then
Return f
Else
Return _footerRow2
End If
End Get
End Property
Protected _headerRow2 As GridViewRow
Public Overloads Overrides ReadOnly Property HeaderRow() As GridViewRow
Get
Dim h As GridViewRow = MyBase.HeaderRow
If h IsNot Nothing Then
Return h
Else
Return Me._headerRow2
End If
End Get
End Property
#End Region
Protected Overloads Overrides Function CreateChildControls(ByVal dataSource As System.Collections.IEnumerable, ByVal dataBinding As Boolean) As Integer
Dim numRows As Integer = MyBase.CreateChildControls(dataSource, dataBinding)
'no data rows created, create empty table if enabled
If numRows = 0 AndAlso ShowWhenEmpty Then
'create table
Dim table As New Table()
table.ID = Me.ID
'convert the exisiting columns into an array and initialize
Dim fields As DataControlField() = New DataControlField(Me.Columns.Count - 1) {}
Me.Columns.CopyTo(fields, 0)
'create a new header row
If Me.ShowHeader Then
_headerRow2 = MyBase.CreateRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal)
Me.InitializeRow(_headerRow2, fields)
table.Rows.Add(_headerRow2)
End If
'create the empty row
Dim emptyRow As New GridViewRow(-1, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal)
Dim cell As New TableCell()
cell.ColumnSpan = Me.Columns.Count
'cell.Width = Unit.Percentage(100)
If Not [String].IsNullOrEmpty(EmptyDataText) Then
cell.Controls.Add(New LiteralControl(EmptyDataText))
End If
If Me.EmptyDataTemplate IsNot Nothing Then
EmptyDataTemplate.InstantiateIn(cell)
End If
emptyRow.Cells.Add(cell)
table.Rows.Add(emptyRow)
'create footer row
If Me.ShowFooter Then
_footerRow2 = MyBase.CreateRow(-1, -1, DataControlRowType.Footer, DataControlRowState.Normal)
Me.InitializeRow(_footerRow2, fields)
table.Rows.Add(_footerRow2)
End If
Me.Controls.Clear()
Me.Controls.Add(table)
End If
Return numRows
End Function
End Class
End Namespace
Thanks
Chris