change HeaderText of DataGrid in PreRender().

Discussion in 'ASP .Net Datagrid Control' started by Andy Wang, May 12, 2004.

  1. Andy Wang

    Andy Wang Guest

    I need change HeaderText of DataGrid in PreRender(). But it seems
    doesn't work.
    The code example like this:

    Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
    grdPortals.Columns(1).HeaderText = "Title"
    End Sub

    I can see the value changed correctly when I was in debug mode, but it
    doesn't show on the page. It always the original value.

    When I try to change the same HeaderText of DataGrid, it works fine.
    The code is below,

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    grdPortals.Columns(1).HeaderText = "Title"
    End Sub

    Does this means the HeaderText of DataGrid can only be changed in
    Page_Load()? And cannot be changed in PreRender()? Is there any way to
    change it in PreRender()?

    Thanks,

    Andy
    Andy Wang, May 12, 2004
    #1
    1. Advertising

  2. Andy, the reason is because when the DataGrid's DataBind() method is
    called, it build a control hierarchy for the DataGrid - a Table Web
    control, that contains rows corresponding to the DataGrid's DataSource.
    The Table's header columns have their text set based on the
    corresponding DataGridColumn's HeaderText property.

    Now, it works in the Page_LOad event because you're setting this
    HeaderText property BEFORE you're calling DataBind. So, when the Table
    is build, its header columns' text are set to the changed value.

    If, however, you wait until the PreRender stage, you have already called
    DataBind. So, the table's header columns' text are already set based on
    the HeaderText property of the DataGridColumns. By changing the
    DataGridColumns HeaderText property in PreRender, you're not affecting
    the actual header text content in the control hierarchy that is rendered.

    I really hope this makes sense! :)

    --

    Scott Mitchell

    http://www.4GuysFromRolla.com
    http://www.ASPFAQs.com
    http://www.ASPMessageboard.com

    * When you think ASP, think 4GuysFromRolla.com!
    Scott Mitchell [MVP], May 13, 2004
    #2
    1. Advertising

  3. Hmmm

    What's the difference between the header and the data cells? Data cells
    value are also already set on DataBind stage out of Cells collection for
    every row. Nevertheless, when you change Cells in prerender, the
    resulting table does pick up the changes. Why should not it work for the
    headers?

    Eliyahu

    "Scott Mitchell [MVP]" <> wrote in message
    news:...
    > Andy, the reason is because when the DataGrid's DataBind() method is
    > called, it build a control hierarchy for the DataGrid - a Table Web
    > control, that contains rows corresponding to the DataGrid's DataSource.
    > The Table's header columns have their text set based on the
    > corresponding DataGridColumn's HeaderText property.
    >
    > Now, it works in the Page_LOad event because you're setting this
    > HeaderText property BEFORE you're calling DataBind. So, when the Table
    > is build, its header columns' text are set to the changed value.
    >
    > If, however, you wait until the PreRender stage, you have already called
    > DataBind. So, the table's header columns' text are already set based on
    > the HeaderText property of the DataGridColumns. By changing the
    > DataGridColumns HeaderText property in PreRender, you're not affecting
    > the actual header text content in the control hierarchy that is rendered.
    >
    > I really hope this makes sense! :)
    >
    > --
    >
    > Scott Mitchell
    >
    > http://www.4GuysFromRolla.com
    > http://www.ASPFAQs.com
    > http://www.ASPMessageboard.com
    >
    > * When you think ASP, think 4GuysFromRolla.com!
    Eliyahu Goldin, May 13, 2004
    #3
  4. Andy Wang

    Andy Wang Guest

    Scott,
    Thanks a lot for you explaination.

    Then I will have problem.
    My work is
    1. If session changed, reload the web form
    2. Check the session
    3. Do Session stuff()
    {...
    Set the HeardText of DataGrid
    ...}

    But the event order in the ASP.Net when I reload the page caused by
    session changed is
    1. Page_Load
    2. Session changing take effect
    3. PreRender()

    In order to check session after it take effect, I have to put
    2. Check the session
    3. Do Session stuff()
    {...
    Set the HeardText of DataGrid
    ...}

    in PreRender.

    But accoring your explain, the Set the HeardText of DataGrid should
    be put in Page_Load, Then at that time the change of session haven't
    took effect yet. Then these two will be confict.

    I have tried another way, change the control in TemplateControl
    Class(I thhought it's in memory), the code is below. But still cannot
    change it. I think the reason is the same as your explaination

    Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
    ChangeInDataGrid(Me)
    End Sub

    Public Sub ChangeInDataGrid(ByRef vPage As
    System.Web.UI.TemplateControl)
    Dim vControl As Object
    Dim vTypeFullName As String =
    vPage.GetType.BaseType.FullName
    Dim vItems As ControlCollection
    For Each vControl In vPage.Controls
    If TypeOf (vControl) Is
    System.Web.UI.WebControls.DataGrid Then
    vControl.Columns(1).HeaderText = "AAA"
    End If
    Next
    End Sub


    Is there any other event handler after Page_Load, and before
    DataBinding of the DataGrid that I can use? Or Os there any other
    method to solve this problem?

    Thanks a lot

    Andy
    "Scott Mitchell [MVP]" <> wrote in message news:<>...
    > Andy, the reason is because when the DataGrid's DataBind() method is
    > called, it build a control hierarchy for the DataGrid - a Table Web
    > control, that contains rows corresponding to the DataGrid's DataSource.
    > The Table's header columns have their text set based on the
    > corresponding DataGridColumn's HeaderText property.
    >
    > Now, it works in the Page_LOad event because you're setting this
    > HeaderText property BEFORE you're calling DataBind. So, when the Table
    > is build, its header columns' text are set to the changed value.
    >
    > If, however, you wait until the PreRender stage, you have already called
    > DataBind. So, the table's header columns' text are already set based on
    > the HeaderText property of the DataGridColumns. By changing the
    > DataGridColumns HeaderText property in PreRender, you're not affecting
    > the actual header text content in the control hierarchy that is rendered.
    >
    > I really hope this makes sense! :)
    >
    > --
    >
    > Scott Mitchell
    >
    > http://www.4GuysFromRolla.com
    > http://www.ASPFAQs.com
    > http://www.ASPMessageboard.com
    >
    > * When you think ASP, think 4GuysFromRolla.com!
    Andy Wang, May 13, 2004
    #4
  5. Eliyahu Goldin wrote:
    > What's the difference between the header and the data cells? Data cells
    > value are also already set on DataBind stage out of Cells collection for
    > every row. Nevertheless, when you change Cells in prerender, the
    > resulting table does pick up the changes. Why should not it work for the
    > headers?


    Eliyahu, if you are changing the cells in the Table, then, yes, you can
    do this in PreRender. But what Andy was doing was changing the
    HeaderText property of the DataGridColumn - NOT the header text of the
    Table column. After the DataGrid's DataBind() method has been called
    (which was presumably done in Page_Load or in a Web control's event
    handler), it's "too late," as the value of the DataGridColumn's
    HeaderText property has already been assigned to the corresponding
    header of the table column.

    Make sense?



    > "Scott Mitchell [MVP]" <> wrote in message
    > news:...
    >
    >>Andy, the reason is because when the DataGrid's DataBind() method is
    >>called, it build a control hierarchy for the DataGrid - a Table Web
    >>control, that contains rows corresponding to the DataGrid's DataSource.
    >> The Table's header columns have their text set based on the
    >>corresponding DataGridColumn's HeaderText property.
    >>
    >>Now, it works in the Page_LOad event because you're setting this
    >>HeaderText property BEFORE you're calling DataBind. So, when the Table
    >>is build, its header columns' text are set to the changed value.
    >>
    >>If, however, you wait until the PreRender stage, you have already called
    >>DataBind. So, the table's header columns' text are already set based on
    >>the HeaderText property of the DataGridColumns. By changing the
    >>DataGridColumns HeaderText property in PreRender, you're not affecting
    >>the actual header text content in the control hierarchy that is rendered.
    >>
    >>I really hope this makes sense! :)
    >>
    >>--
    >>
    >> Scott Mitchell
    >>
    >> http://www.4GuysFromRolla.com
    >> http://www.ASPFAQs.com
    >> http://www.ASPMessageboard.com
    >>
    >>* When you think ASP, think 4GuysFromRolla.com!

    >
    >
    >



    --

    Scott Mitchell

    http://www.4GuysFromRolla.com
    http://www.ASPFAQs.com
    http://www.ASPMessageboard.com

    * When you think ASP, think 4GuysFromRolla.com!
    Scott Mitchell [MVP], May 13, 2004
    #5
  6. Hi Scott,

    I am using extensively Pretender in my applications. I am doing mostly cell
    text formatting and I access cells like this:

    item.Cells

    where item is a parameter from PreRender handler. The type of item is
    System.Web.UI.WebControls.DataGridItem and item.Cells produces an object
    of type System.Web.UI.WebControls.TableCell. It is not the resulting html
    <td>, it is a server side web control, the same as HeaderText. So I am still
    not convinced.

    Regards,

    Eliyahu


    "Scott Mitchell [MVP]" <> wrote in message
    news:...
    > Eliyahu Goldin wrote:
    > > What's the difference between the header and the data cells? Data cells
    > > value are also already set on DataBind stage out of Cells collection for
    > > every row. Nevertheless, when you change Cells in prerender, the
    > > resulting table does pick up the changes. Why should not it work for the
    > > headers?

    >
    > Eliyahu, if you are changing the cells in the Table, then, yes, you can
    > do this in PreRender. But what Andy was doing was changing the
    > HeaderText property of the DataGridColumn - NOT the header text of the
    > Table column. After the DataGrid's DataBind() method has been called
    > (which was presumably done in Page_Load or in a Web control's event
    > handler), it's "too late," as the value of the DataGridColumn's
    > HeaderText property has already been assigned to the corresponding
    > header of the table column.
    >
    > Make sense?
    >
    >
    >
    > > "Scott Mitchell [MVP]" <> wrote in message
    > > news:...
    > >
    > >>Andy, the reason is because when the DataGrid's DataBind() method is
    > >>called, it build a control hierarchy for the DataGrid - a Table Web
    > >>control, that contains rows corresponding to the DataGrid's DataSource.
    > >> The Table's header columns have their text set based on the
    > >>corresponding DataGridColumn's HeaderText property.
    > >>
    > >>Now, it works in the Page_LOad event because you're setting this
    > >>HeaderText property BEFORE you're calling DataBind. So, when the Table
    > >>is build, its header columns' text are set to the changed value.
    > >>
    > >>If, however, you wait until the PreRender stage, you have already called
    > >>DataBind. So, the table's header columns' text are already set based on
    > >>the HeaderText property of the DataGridColumns. By changing the
    > >>DataGridColumns HeaderText property in PreRender, you're not affecting
    > >>the actual header text content in the control hierarchy that is

    rendered.
    > >>
    > >>I really hope this makes sense! :)
    > >>
    > >>--
    > >>
    > >> Scott Mitchell
    > >>
    > >> http://www.4GuysFromRolla.com
    > >> http://www.ASPFAQs.com
    > >> http://www.ASPMessageboard.com
    > >>
    > >>* When you think ASP, think 4GuysFromRolla.com!

    > >
    > >
    > >

    >
    >
    > --
    >
    > Scott Mitchell
    >
    > http://www.4GuysFromRolla.com
    > http://www.ASPFAQs.com
    > http://www.ASPMessageboard.com
    >
    > * When you think ASP, think 4GuysFromRolla.com!
    Eliyahu Goldin, May 14, 2004
    #6
  7. Actually, item is not a parameter from the handler. It's a pointer to grid
    items from statement
    foreach (item in myGrid.Items){...}

    There is nothing special in my code. You can make your own item.Cells =
    "something"; and it will work. I did test setting HeaderText and it indeed
    didn't work. The HeaderText property does get changed, I confirmed it with
    the debugger. But the change doesn't propagate to the client. I guess there
    must be something in state management preventing saving the change.

    Eliyahu

    "Scott Mitchell [MVP]" <> wrote in message
    news:...
    > Eliyahu Goldin wrote:
    > > I am using extensively Pretender in my applications. I am doing mostly

    cell
    > > text formatting and I access cells like this:
    > >
    > > item.Cells
    > >
    > > where item is a parameter from PreRender handler. The type of item is
    > > System.Web.UI.WebControls.DataGridItem and item.Cells produces an

    object
    > > of type System.Web.UI.WebControls.TableCell. It is not the resulting

    html
    > > <td>, it is a server side web control, the same as HeaderText. So I am

    still
    > > not convinced.

    >
    > Fair enough. Could you provide the code you are using here? I'd be
    > interested in seeing it.
    >
    > Thanks!
    >
    > --
    >
    > Scott Mitchell
    >
    > http://www.4GuysFromRolla.com
    > http://www.ASPFAQs.com
    > http://www.ASPMessageboard.com
    >
    > * When you think ASP, think 4GuysFromRolla.com!
    Eliyahu Goldin, May 16, 2004
    #7
    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. Andy Wang
    Replies:
    0
    Views:
    477
    Andy Wang
    May 12, 2004
  2. Øyvind Isaksen

    Change HeaderText in datagrid

    Øyvind Isaksen, Nov 6, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    3,931
    Eliyahu Goldin
    Nov 6, 2005
  3. Joe Rattz

    HeaderText changed during PreRender does not render

    Joe Rattz, Nov 6, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    138
    Joe Rattz
    Nov 6, 2003
  4. Ravi Gudlavalleti

    How do I change the headertext of a datagrid column programmatically

    Ravi Gudlavalleti, Jun 17, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    197
    Ken Cox [Microsoft MVP]
    Jun 18, 2004
  5. Sangra M via .NET 247

    Change HeaderText of datagrid after binding

    Sangra M via .NET 247, Mar 14, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    286
    Sylvain Lafontaine
    Mar 14, 2005
Loading...

Share This Page