Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net Datagrid Control (http://www.velocityreviews.com/forums/f60-asp-net-datagrid-control.html)
-   -   change HeaderText of DataGrid in PreRender(). (http://www.velocityreviews.com/forums/t760583-change-headertext-of-datagrid-in-prerender.html)

Andy Wang 05-12-2004 08:57 PM

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

Scott Mitchell [MVP] 05-12-2004 11:22 PM

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

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

Eliyahu Goldin 05-13-2004 08:49 AM

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
> mitchell@4guysfromrolla.com
> http://www.4GuysFromRolla.com
> http://www.ASPFAQs.com
> http://www.ASPMessageboard.com
>
> * When you think ASP, think 4GuysFromRolla.com!




Andy Wang 05-13-2004 04:02 PM

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
> mitchell@4guysfromrolla.com
> http://www.4GuysFromRolla.com
> http://www.ASPFAQs.com
> http://www.ASPMessageboard.com
>
> * When you think ASP, think 4GuysFromRolla.com!


Scott Mitchell [MVP] 05-13-2004 08:51 PM

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
>> mitchell@4guysfromrolla.com
>> http://www.4GuysFromRolla.com
>> http://www.ASPFAQs.com
>> http://www.ASPMessageboard.com
>>
>>* When you think ASP, think 4GuysFromRolla.com!

>
>
>



--

Scott Mitchell
mitchell@4guysfromrolla.com
http://www.4GuysFromRolla.com
http://www.ASPFAQs.com
http://www.ASPMessageboard.com

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

Eliyahu Goldin 05-14-2004 03:43 PM

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
> >> mitchell@4guysfromrolla.com
> >> http://www.4GuysFromRolla.com
> >> http://www.ASPFAQs.com
> >> http://www.ASPMessageboard.com
> >>
> >>* When you think ASP, think 4GuysFromRolla.com!

> >
> >
> >

>
>
> --
>
> Scott Mitchell
> mitchell@4guysfromrolla.com
> http://www.4GuysFromRolla.com
> http://www.ASPFAQs.com
> http://www.ASPMessageboard.com
>
> * When you think ASP, think 4GuysFromRolla.com!




Eliyahu Goldin 05-16-2004 09:49 AM

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
> mitchell@4guysfromrolla.com
> http://www.4GuysFromRolla.com
> http://www.ASPFAQs.com
> http://www.ASPMessageboard.com
>
> * When you think ASP, think 4GuysFromRolla.com!





All times are GMT. The time now is 11:53 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.