change HeaderText of DataGrid in PreRender().

A

Andy Wang

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
 
S

Scott Mitchell [MVP]

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
(e-mail address removed)
http://www.4GuysFromRolla.com
http://www.ASPFAQs.com
http://www.ASPMessageboard.com

* When you think ASP, think 4GuysFromRolla.com!
 
E

Eliyahu Goldin

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
 
A

Andy Wang

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
 
S

Scott Mitchell [MVP]

Eliyahu said:
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
(e-mail address removed)
http://www.4GuysFromRolla.com
http://www.ASPFAQs.com
http://www.ASPMessageboard.com

* When you think ASP, think 4GuysFromRolla.com!
 
E

Eliyahu Goldin

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 said:
Eliyahu said:
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
(e-mail address removed)
http://www.4GuysFromRolla.com
http://www.ASPFAQs.com
http://www.ASPMessageboard.com

* When you think ASP, think 4GuysFromRolla.com!
 
E

Eliyahu Goldin

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 said:
Eliyahu said:
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
(e-mail address removed)
http://www.4GuysFromRolla.com
http://www.ASPFAQs.com
http://www.ASPMessageboard.com

* When you think ASP, think 4GuysFromRolla.com!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top