DataGrid subclass - design time problem

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
 
J

Jay

I found the problem, but not how to solve it. The problem is with the
following property, and specifically the line: HasData =
aDataSource.HasRows. Everything works fine in runtime mode. In design time
I get an error. If I quote out that line and hard code HasData everything
works fine in design time. Supposedly the If (HttpContext.Current is
DBNull.value) detects desgin time but it does not seem to help

HELP. Jay

Public Overrides Property DataSource() As Object

Get

Return (MyBase.DataSource)

End Get

Set(ByVal aDataSource As Object)

If (HttpContext.Current Is DBNull.Value) Then

HasData = False

Else

'HasData = aDataSource.HasRows

HasData = True

End If

MyBase.DataSource = aDataSource

End Set

End Property
 
J

Jay

I know have found the solution!

The following statement:
If (HttpContext.Current Is DBNull.Value) Then

should be:
If (HttpContext.Current Is Nothing) Then

I found an article on detecting DesignMode. It was written in C# and used
(HttpContext.Current == null). It turns out that you must use NOTHING in
vb.net.

Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top