J
Jay
I created a custom control which is a simple subclass of the DataGrid. I
wanted to display a message when the DataSource is empty. Right now I am
assuming the datasource is a datareader.
The problem is in design mode. I get the error "Error in displaying...".
This is caused by the DataSource override property trying to access the data
source at design time. How do I set things up to avoid this design time
problem.
Thanks. Jay
Imports System.ComponentModel
Imports System.Web.UI
Public Class MyDataGrid
Inherits System.Web.UI.WebControls.DataGrid
Public ShowMyHeader As Boolean = True
Public NoDataMessage As String = "No Data Is Available!"
Private HasData As Boolean
'Public Overrides Property DataSource() As Object
' Get
' Return (MyBase.DataSource)
' End Get
' Set(ByVal aDataSource As Object)
' HasData = aDataSource.HasRows
' MyBase.DataSource = aDataSource
' End Set
'End Property
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
If HasData Then
MyBase.Render(writer)
Else
Dim Message As New System.Web.UI.WebControls.Label
Message.Text = NoDataMessage
Message.Font.Bold = True
Message.Font.Size = System.Web.UI.WebControls.FontUnit.Point(10)
Message.Font.Name = "arial"
Message.ForeColor = System.Drawing.Color.Red
Me.ShowHeader = Me.ShowMyHeader
writer.AddAttribute(HtmlTextWriterAttribute.Border, "0")
writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "0")
writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0")
writer.RenderBeginTag(HtmlTextWriterTag.Table)
writer.RenderBeginTag(HtmlTextWriterTag.Tr)
writer.AddAttribute(HtmlTextWriterAttribute.Align, "center")
writer.RenderBeginTag(HtmlTextWriterTag.Td)
MyBase.Render(writer)
writer.RenderEndTag() ' Td
writer.RenderEndTag() ' Tr
writer.RenderBeginTag(HtmlTextWriterTag.Tr)
writer.AddAttribute(HtmlTextWriterAttribute.Align, "center")
writer.AddAttribute(HtmlTextWriterAttribute.Valign, "bottom")
writer.AddAttribute(HtmlTextWriterAttribute.Height, "30")
writer.RenderBeginTag(HtmlTextWriterTag.Td)
Message.RenderControl(writer)
writer.RenderEndTag() ' Td
writer.RenderEndTag() ' Tr
writer.RenderEndTag() ' Table
End If
End Sub
End Class
wanted to display a message when the DataSource is empty. Right now I am
assuming the datasource is a datareader.
The problem is in design mode. I get the error "Error in displaying...".
This is caused by the DataSource override property trying to access the data
source at design time. How do I set things up to avoid this design time
problem.
Thanks. Jay
Imports System.ComponentModel
Imports System.Web.UI
Public Class MyDataGrid
Inherits System.Web.UI.WebControls.DataGrid
Public ShowMyHeader As Boolean = True
Public NoDataMessage As String = "No Data Is Available!"
Private HasData As Boolean
'Public Overrides Property DataSource() As Object
' Get
' Return (MyBase.DataSource)
' End Get
' Set(ByVal aDataSource As Object)
' HasData = aDataSource.HasRows
' MyBase.DataSource = aDataSource
' End Set
'End Property
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
If HasData Then
MyBase.Render(writer)
Else
Dim Message As New System.Web.UI.WebControls.Label
Message.Text = NoDataMessage
Message.Font.Bold = True
Message.Font.Size = System.Web.UI.WebControls.FontUnit.Point(10)
Message.Font.Name = "arial"
Message.ForeColor = System.Drawing.Color.Red
Me.ShowHeader = Me.ShowMyHeader
writer.AddAttribute(HtmlTextWriterAttribute.Border, "0")
writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "0")
writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0")
writer.RenderBeginTag(HtmlTextWriterTag.Table)
writer.RenderBeginTag(HtmlTextWriterTag.Tr)
writer.AddAttribute(HtmlTextWriterAttribute.Align, "center")
writer.RenderBeginTag(HtmlTextWriterTag.Td)
MyBase.Render(writer)
writer.RenderEndTag() ' Td
writer.RenderEndTag() ' Tr
writer.RenderBeginTag(HtmlTextWriterTag.Tr)
writer.AddAttribute(HtmlTextWriterAttribute.Align, "center")
writer.AddAttribute(HtmlTextWriterAttribute.Valign, "bottom")
writer.AddAttribute(HtmlTextWriterAttribute.Height, "30")
writer.RenderBeginTag(HtmlTextWriterTag.Td)
Message.RenderControl(writer)
writer.RenderEndTag() ' Td
writer.RenderEndTag() ' Tr
writer.RenderEndTag() ' Table
End If
End Sub
End Class