DataGrid subclass - design time problem

Discussion in 'ASP .Net Building Controls' started by Jay, Mar 4, 2005.

  1. Jay

    Jay Guest

    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
     
    Jay, Mar 4, 2005
    #1
    1. Advertising

  2. Jay

    Jay Guest

    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




    "Jay" <> wrote in message
    news:ut%...
    >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
    >
    >
     
    Jay, Mar 4, 2005
    #2
    1. Advertising

  3. Jay

    Jay Guest

    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
     
    Jay, Mar 4, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jstorta
    Replies:
    3
    Views:
    475
    jstorta
    Feb 20, 2006
  2. Brett Wiltshire
    Replies:
    0
    Views:
    350
    Brett Wiltshire
    Aug 10, 2007
  3. S.Volkov
    Replies:
    2
    Views:
    242
    S.Volkov
    Mar 12, 2006
  4. Trans
    Replies:
    8
    Views:
    351
    Robert Klemme
    Oct 23, 2008
  5. Fab

    Subclass of subclass

    Fab, Aug 9, 2012, in forum: C++
    Replies:
    0
    Views:
    415
Loading...

Share This Page