change HeaderText of DataGrid in PreRender().

Ask a Question related to ASP.NET Data Grid Control, Design and Development.

  1. #1

    Default change HeaderText of DataGrid in PreRender().

    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 Guest

  2. Similar Questions and Discussions

    1. Change HeaderText of datagrid after binding
      (Type your message here) -------------------------------- From: Sangra Hi, I got a datagrid which I want to change the headertext after...
    2. How do I change the headertext of a datagrid column programmatically
      Hi, I have a datagrid to view the results from a database. I am using autogenerate because the number of columns returned from the database...
    3. DataGrid Dynamic HeaderText
      Hell How can I change the headerText of an AutoGenerateColumns DataGrid myDataGrid.DataSource=ds.Tables.DefaultView myDataGrid.DataBind()...
    4. HeaderText changed during PreRender does not render
      I am trying to programatically change my HeaderText. I can change the foreground and background colors and everything render fine. But, if I...
    5. How to change datagrid column HeaderText from C#
      You can do thisn DataGrid_ItemCreated Event HTH Prasad "MarkMurphy" <murphy@murphysw.com> wrote in message...
  3. #2

    Default Re: change HeaderText of DataGrid in PreRender().

    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
    [email]mitchell@4guysfromrolla.com[/email]
    [url]http://www.4GuysFromRolla.com[/url]
    [url]http://www.ASPFAQs.com[/url]
    [url]http://www.ASPMessageboard.com[/url]

    * When you think ASP, think 4GuysFromRolla.com!
    Scott Mitchell [MVP] Guest

  4. #3

    Default Re: change HeaderText of DataGrid in PreRender().

    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[i] in prerender, the
    resulting table does pick up the changes. Why should not it work for the
    headers?

    Eliyahu

    "Scott Mitchell [MVP]" <mitchell@4guysfromrolla.com> wrote in message
    news:40A2B1AB.6030201@4guysfromrolla.com...
    > 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
    > [email]mitchell@4guysfromrolla.com[/email]
    > [url]http://www.4GuysFromRolla.com[/url]
    > [url]http://www.ASPFAQs.com[/url]
    > [url]http://www.ASPMessageboard.com[/url]
    >
    > * When you think ASP, think 4GuysFromRolla.com!

    Eliyahu Goldin Guest

  5. #4

    Default Re: change HeaderText of DataGrid in PreRender().

    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]" <mitchell@4guysfromrolla.com> wrote in message news:<40A2B1AB.6030201@4guysfromrolla.com>...
    > 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
    > [email]mitchell@4guysfromrolla.com[/email]
    > [url]http://www.4GuysFromRolla.com[/url]
    > [url]http://www.ASPFAQs.com[/url]
    > [url]http://www.ASPMessageboard.com[/url]
    >
    > * When you think ASP, think 4GuysFromRolla.com!
    Andy Wang Guest

  6. #5

    Default Re: change HeaderText of DataGrid in PreRender().

    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[i] 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]" <mitchell@4guysfromrolla.com> wrote in message
    > news:40A2B1AB.6030201@4guysfromrolla.com...
    >
    >>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
    >> [email]mitchell@4guysfromrolla.com[/email]
    >> [url]http://www.4GuysFromRolla.com[/url]
    >> [url]http://www.ASPFAQs.com[/url]
    >> [url]http://www.ASPMessageboard.com[/url]
    >>
    >>* When you think ASP, think 4GuysFromRolla.com!
    >
    >
    >

    --

    Scott Mitchell
    [email]mitchell@4guysfromrolla.com[/email]
    [url]http://www.4GuysFromRolla.com[/url]
    [url]http://www.ASPFAQs.com[/url]
    [url]http://www.ASPMessageboard.com[/url]

    * When you think ASP, think 4GuysFromRolla.com!
    Scott Mitchell [MVP] Guest

  7. #6

    Default Re: change HeaderText of DataGrid in PreRender().

    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[i]
    >
    > where item is a parameter from PreRender handler. The type of item is
    > System.Web.UI.WebControls.DataGridItem and item.Cells[i] 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
    [email]mitchell@4guysfromrolla.com[/email]
    [url]http://www.4GuysFromRolla.com[/url]
    [url]http://www.ASPFAQs.com[/url]
    [url]http://www.ASPMessageboard.com[/url]

    * When you think ASP, think 4GuysFromRolla.com!
    Scott Mitchell [MVP] Guest

  8. #7

    Default Re: change HeaderText of DataGrid in PreRender().

    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[i]

    where item is a parameter from PreRender handler. The type of item is
    System.Web.UI.WebControls.DataGridItem and item.Cells[i] 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]" <mitchell@4guysfromrolla.com> wrote in message
    news:40A3DFDF.8000908@4guysfromrolla.com...
    > 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[i] 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]" <mitchell@4guysfromrolla.com> wrote in message
    > > news:40A2B1AB.6030201@4guysfromrolla.com...
    > >
    > >>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
    > >> [email]mitchell@4guysfromrolla.com[/email]
    > >> [url]http://www.4GuysFromRolla.com[/url]
    > >> [url]http://www.ASPFAQs.com[/url]
    > >> [url]http://www.ASPMessageboard.com[/url]
    > >>
    > >>* When you think ASP, think 4GuysFromRolla.com!
    > >
    > >
    > >
    >
    >
    > --
    >
    > Scott Mitchell
    > [email]mitchell@4guysfromrolla.com[/email]
    > [url]http://www.4GuysFromRolla.com[/url]
    > [url]http://www.ASPFAQs.com[/url]
    > [url]http://www.ASPMessageboard.com[/url]
    >
    > * When you think ASP, think 4GuysFromRolla.com!

    Eliyahu Goldin Guest

  9. #8

    Default Re: change HeaderText of DataGrid in PreRender().

    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[i] =
    "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]" <mitchell@4guysfromrolla.com> wrote in message
    news:40A4DC29.6010004@4guysfromrolla.com...
    > 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[i]
    > >
    > > where item is a parameter from PreRender handler. The type of item is
    > > System.Web.UI.WebControls.DataGridItem and item.Cells[i] 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
    > [email]mitchell@4guysfromrolla.com[/email]
    > [url]http://www.4GuysFromRolla.com[/url]
    > [url]http://www.ASPFAQs.com[/url]
    > [url]http://www.ASPMessageboard.com[/url]
    >
    > * When you think ASP, think 4GuysFromRolla.com!

    Eliyahu Goldin Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139