inherited DataGrid failed to render

Discussion in 'ASP .Net' started by Frank, Aug 12, 2004.

  1. Frank

    Frank Guest

    Hi all,

    I want to derive a class from DataGrid so that I can have
    a label on the top.
    But when I tried to add the control to my form, I got an
    error "Specified cast is not valid" on the line:
    MyBase.Render(w) in the Render function.

    Thanks a lot.
    Frank

    Here is my simple code.

    Private Sub Page_Load(ByVal sender As System.Object,
    ByVal e As System.EventArgs) Handles MyBase.Load

    Dim g As New Grid2
    Me.Form1.Controls.Add(g)
    End Sub
    Public Class Grid2
    Inherits DataGrid
    Dim _label1 As New Label

    Protected Overrides Sub OnInit(ByVal e As EventArgs)
    Me.Controls.Add(Me._label1)
    End Sub
    Public ReadOnly Property Label1() As Label
    Get
    Return Me._label1
    End Get
    End Property
    Protected Overrides Sub Render(ByVal w As
    HtmlTextWriter)
    MyBase.Render(w)
    Me._label1.RenderControl(w)
    End Sub
    End Class
     
    Frank, Aug 12, 2004
    #1
    1. Advertising

  2. Frank

    chanmmn Guest

    You cannot do that I think you need to create your own user control.

    chanmm
    "Frank" <> wrote in message
    news:4ece01c48082$19297e30$...
    > Hi all,
    >
    > I want to derive a class from DataGrid so that I can have
    > a label on the top.
    > But when I tried to add the control to my form, I got an
    > error "Specified cast is not valid" on the line:
    > MyBase.Render(w) in the Render function.
    >
    > Thanks a lot.
    > Frank
    >
    > Here is my simple code.
    >
    > Private Sub Page_Load(ByVal sender As System.Object,
    > ByVal e As System.EventArgs) Handles MyBase.Load
    >
    > Dim g As New Grid2
    > Me.Form1.Controls.Add(g)
    > End Sub
    > Public Class Grid2
    > Inherits DataGrid
    > Dim _label1 As New Label
    >
    > Protected Overrides Sub OnInit(ByVal e As EventArgs)
    > Me.Controls.Add(Me._label1)
    > End Sub
    > Public ReadOnly Property Label1() As Label
    > Get
    > Return Me._label1
    > End Get
    > End Property
    > Protected Overrides Sub Render(ByVal w As
    > HtmlTextWriter)
    > MyBase.Render(w)
    > Me._label1.RenderControl(w)
    > End Sub
    > End Class
     
    chanmmn, Aug 12, 2004
    #2
    1. Advertising

  3. Frank

    Scott Allen Guest

    Hi Frank:

    You can inherit from a DataGrid.

    In this case though, I think you might want to look at hooking the
    ItemCreated event, look for when the grid is creating a header
    section, and add an extra row to the data grid with your text.

    This article demonstrates many tricks you can use:
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndive/html/data01102002.asp

    --
    Scott
    http://www.OdeToCode.com

    On Thu, 12 Aug 2004 08:36:12 -0700, "Frank" <>
    wrote:

    >Hi all,
    >
    >I want to derive a class from DataGrid so that I can have
    >a label on the top.
    >But when I tried to add the control to my form, I got an
    >error "Specified cast is not valid" on the line:
    >MyBase.Render(w) in the Render function.
    >
    >Thanks a lot.
    >Frank
    >
    >Here is my simple code.
    >
    >Private Sub Page_Load(ByVal sender As System.Object,
    >ByVal e As System.EventArgs) Handles MyBase.Load
    >
    > Dim g As New Grid2
    > Me.Form1.Controls.Add(g)
    >End Sub
    >Public Class Grid2
    > Inherits DataGrid
    > Dim _label1 As New Label
    >
    > Protected Overrides Sub OnInit(ByVal e As EventArgs)
    > Me.Controls.Add(Me._label1)
    > End Sub
    > Public ReadOnly Property Label1() As Label
    > Get
    > Return Me._label1
    > End Get
    > End Property
    > Protected Overrides Sub Render(ByVal w As
    >HtmlTextWriter)
    > MyBase.Render(w)
    > Me._label1.RenderControl(w)
    > End Sub
    >End Class
     
    Scott Allen, Aug 12, 2004
    #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. Lau Lei Cheong

    Page.Render do not render complete page

    Lau Lei Cheong, May 15, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,121
    Lau Lei Cheong
    May 15, 2004
  2. sck10
    Replies:
    6
    Views:
    961
    sck10
    Sep 1, 2006
  3. Frank

    render failed on inherited DataGrid

    Frank, Aug 12, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    121
  4. Glenn Gillen
    Replies:
    0
    Views:
    321
    Glenn Gillen
    Nov 17, 2006
  5. 7stud --
    Replies:
    11
    Views:
    468
    7stud --
    Nov 9, 2007
Loading...

Share This Page