PrepareControlHierarchy

Discussion in 'ASP .Net Building Controls' started by Faizan Ahmed, Dec 27, 2004.

  1. Faizan Ahmed

    Faizan Ahmed Guest

    Hello All,

    I have yet another problem,
    I've derived a control from Datagrid,

    Now in the OnInit() method i Created a button control, added it to a
    HtmlTable and added the HtmlTable to my Composite control's Control
    collection.

    The heirarchy is something like this
    CustomControl C
    HtmlTable T
    Button B
    i.e.
    H.Controls.Add(B)
    C.Controls.Add(T)

    The render method of my control is as follows

    Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter)



    MyBase.Render(output)

    End Sub


    This statement gives an error in DataGrid.PrepareControlHierarchy() method.
    If i remove HtmlTable from the control collection of my control then it
    works fine, but then the button control in the table does not raise any
    events.

    Can any one help??Mr. Saunders?
    Thanks in advance people,

    Best Regards
     
    Faizan Ahmed, Dec 27, 2004
    #1
    1. Advertising

  2. "Faizan Ahmed" <> wrote in message
    news:...
    > Hello All,
    >
    > I have yet another problem,
    > I've derived a control from Datagrid,
    >
    > Now in the OnInit() method i Created a button control, added it to a
    > HtmlTable and added the HtmlTable to my Composite control's Control
    > collection.
    >
    > The heirarchy is something like this
    > CustomControl C
    > HtmlTable T
    > Button B
    > i.e.
    > H.Controls.Add(B)
    > C.Controls.Add(T)
    >
    > The render method of my control is as follows
    >
    > Protected Overrides Sub Render(ByVal output As
    > System.Web.UI.HtmlTextWriter)
    >
    >
    >
    > MyBase.Render(output)
    >
    > End Sub
    >
    >
    > This statement gives an error in DataGrid.PrepareControlHierarchy()
    > method.
    > If i remove HtmlTable from the control collection of my control then it
    > works fine, but then the button control in the table does not raise any
    > events.
    >
    > Can any one help??Mr. Saunders?


    I wouldn't add anything to a DataGrid which it is not expecting to find
    there. After all, how would your control feel if some derived control
    started playing with its Controls collection?

    Instead, you could just not add T, and change your Render method to:

    Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter)
    T.RenderControl(output)
    MyBase.Render(output)
    End Sub

    If you need the table to come after the DataGrid, you can put the
    T.RenderControl after the MyBase.Render.

    John Saunders
     
    John Saunders, Dec 28, 2004
    #2
    1. Advertising

  3. Faizan Ahmed

    Faizan Ahmed Guest

    "John Saunders" <johnwsaundersiii at hotmail.com> wrote in message
    news:...
    > "Faizan Ahmed" <> wrote in message
    > news:...
    > > Hello All,
    > >
    > > I have yet another problem,
    > > I've derived a control from Datagrid,
    > >
    > > Now in the OnInit() method i Created a button control, added it to a
    > > HtmlTable and added the HtmlTable to my Composite control's Control
    > > collection.
    > >
    > > The heirarchy is something like this
    > > CustomControl C
    > > HtmlTable T
    > > Button B
    > > i.e.
    > > H.Controls.Add(B)
    > > C.Controls.Add(T)
    > >
    > > The render method of my control is as follows
    > >
    > > Protected Overrides Sub Render(ByVal output As
    > > System.Web.UI.HtmlTextWriter)
    > >
    > >
    > >
    > > MyBase.Render(output)
    > >
    > > End Sub
    > >
    > >
    > > This statement gives an error in DataGrid.PrepareControlHierarchy()
    > > method.
    > > If i remove HtmlTable from the control collection of my control then it
    > > works fine, but then the button control in the table does not raise any
    > > events.
    > >
    > > Can any one help??Mr. Saunders?

    >
    > I wouldn't add anything to a DataGrid which it is not expecting to find
    > there. After all, how would your control feel if some derived control
    > started playing with its Controls collection?
    >
    > Instead, you could just not add T, and change your Render method to:
    >
    > Protected Overrides Sub Render(ByVal output As

    System.Web.UI.HtmlTextWriter)
    > T.RenderControl(output)
    > MyBase.Render(output)
    > End Sub
    >
    > If you need the table to come after the DataGrid, you can put the
    > T.RenderControl after the MyBase.Render.
    >
    > John Saunders
    >
    >
    >

    But if i implement it this way i wont get any events raised by my control
    T(or its child controls B), because their viewstate wont be mantained by the
    Grid. I think this is a problem many people must've come across but i m not
    sure why there is no solution available for it, may be there is a
    straightforward way of doin such a think and i m moving in a wrong
    direction.
    I've been able to identify the cause of my problem; the
    PrepareControlHeirarchy() function of the DataGrid (which applies styles on
    the control)expects all of the new controls to be added at the end of
    controls collection.

    I found no solution as yet. Any help would be appreciated

    Faizan Ahmed
     
    Faizan Ahmed, Dec 29, 2004
    #3
  4. "Faizan Ahmed" <> wrote in message
    news:%...
    >
    > "John Saunders" <johnwsaundersiii at hotmail.com> wrote in message
    > news:...
    >> "Faizan Ahmed" <> wrote in message
    >> news:...
    >> > Hello All,
    >> >
    >> > I have yet another problem,
    >> > I've derived a control from Datagrid,
    >> >
    >> > Now in the OnInit() method i Created a button control, added it to a
    >> > HtmlTable and added the HtmlTable to my Composite control's Control
    >> > collection.
    >> >
    >> > The heirarchy is something like this
    >> > CustomControl C
    >> > HtmlTable T
    >> > Button B
    >> > i.e.
    >> > H.Controls.Add(B)
    >> > C.Controls.Add(T)
    >> >
    >> > The render method of my control is as follows
    >> >
    >> > Protected Overrides Sub Render(ByVal output As
    >> > System.Web.UI.HtmlTextWriter)
    >> >
    >> >
    >> >
    >> > MyBase.Render(output)
    >> >
    >> > End Sub
    >> >
    >> >
    >> > This statement gives an error in DataGrid.PrepareControlHierarchy()
    >> > method.
    >> > If i remove HtmlTable from the control collection of my control then it
    >> > works fine, but then the button control in the table does not raise any
    >> > events.
    >> >
    >> > Can any one help??Mr. Saunders?

    >>
    >> I wouldn't add anything to a DataGrid which it is not expecting to find
    >> there. After all, how would your control feel if some derived control
    >> started playing with its Controls collection?
    >>
    >> Instead, you could just not add T, and change your Render method to:
    >>
    >> Protected Overrides Sub Render(ByVal output As

    > System.Web.UI.HtmlTextWriter)
    >> T.RenderControl(output)
    >> MyBase.Render(output)
    >> End Sub
    >>
    >> If you need the table to come after the DataGrid, you can put the
    >> T.RenderControl after the MyBase.Render.
    >>
    >> John Saunders
    >>
    >>
    >>

    > But if i implement it this way i wont get any events raised by my control
    > T(or its child controls B), because their viewstate wont be mantained by
    > the
    > Grid. I think this is a problem many people must've come across but i m
    > not
    > sure why there is no solution available for it, may be there is a
    > straightforward way of doin such a think and i m moving in a wrong
    > direction.
    > I've been able to identify the cause of my problem; the
    > PrepareControlHeirarchy() function of the DataGrid (which applies styles
    > on
    > the control)expects all of the new controls to be added at the end of
    > controls collection.


    It may be that you will have to stop inheriting from DataGrid and instead
    create a composite control which contains both the DataGrid and your Table.
    Both the DataGrid and the Table would be in the Controls collection of your
    control, thus ViewState will be maintained.

    John Saunders
     
    John Saunders, Dec 29, 2004
    #4
    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.

Share This Page