Explain Initialization...

C

clintonG

Taking MCAD practice exams questions will ask where to "initialize
controls" or "initialize this or initialize that" but the answer is
never the
Page_Init event -- always Page_Load.

There are also questions that will ask something like how to "modify
the display of the page before it loads" but the answer is never
Page_PreRender -- always Page_Load.

I do not want to go into the exams like a monkey thinking I have to
answer Page_Load to such questions without really understanding
the fine points of these events.

So...

I'm asking for references to articles, documentation, or lucid
commentary
that really puts these events into perspective for me.

--
<%= Clinton Gallagher
A/E/C Consulting, Web Design, e-Commerce Software Development
Wauwatosa, Milwaukee County, Wisconsin USA
NET csgallagher@ REMOVETHISTEXT metromilwaukee.com
URL http://www.metromilwaukee.com/clintongallagher/
 
S

Scott M.

It's actually simple.

During Page_Init, the actual PAGE class is being created. So, you wouldn't
initialize controls there since they don't exist yet (you have to create a
page before controls for that page can be created).

Page_Init could/would be used for initializing components that are not part
of the page but perhaps might be used by the page. But since Page_Load
fires at such a good time during the page creation process, most
initialization of everything happens there.

The other Page events are really for the object itself and you won't find
much use for them yourself.
 
G

Guest

I have found the OnPreRender handle quite useful. It seems to be the only place where you can realistically load any data - at least in cases where the page displays a sortable grid. I have found that the click event on the grid header occurs after the page_load, which means you either have to re-sort all the data in this click event, or just sort it once by loading it into a DataView onPreRender. Or am I missing something?
 
J

John Saunders

Betulus said:
I have found the OnPreRender handle quite useful. It seems to be the only
place where you can realistically load any data - at least in cases where
the page displays a sortable grid. I have found that the click event on the
grid header occurs after the page_load, which means you either have to
re-sort all the data in this click event, or just sort it once by loading it
into a DataView onPreRender. Or am I missing something?

I usually use a DataView as the DataSource for the grid, and change
DataView.Sort to e.SortExpression.
 
G

Guest

Fair enough. I guess setting the sort order after building the DataView is not so efficient, but may be not significantly. I guess that also means storing the DataView at page level scope. I hadn't considered that.

Thanks.
 
S

Scott M.

A DataGrid has a SortCommand event. This event handler is where you should
be sorting the grid contents. You can then use the "e" event argument to
know what criteria to sort by.

Similarly, the DataGrid has PageIndexChanged, DeleteCommand and
UpdateCommand events. These are where you should do the appropriate work.


Betulus said:
I have found the OnPreRender handle quite useful. It seems to be the only
place where you can realistically load any data - at least in cases where
the page displays a sortable grid. I have found that the click event on the
grid header occurs after the page_load, which means you either have to
re-sort all the data in this click event, or just sort it once by loading it
into a DataView onPreRender. Or am I missing something?
 
S

Scott M.

You don't need to store the DataView at all since the grid's viewstate will
show the last data in it.
Once you create the dv and sort it, you set the Grid's datasource to the dv
and you are done.

Sub Page_Load
If Not IsPostBack Then
dg.DataSource = someDataSet
dg.DataBind
Else
dg.DataSource = someDataSet
End If
End Sub

Private Sub dg_PageIndexChanged(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridPageChangedEventArgs)
dg.CurrentPageIndex = e.NewPageIndex
'Now sort the new page of data according to the prior sort criteria
Dim dv As New DataView(dsCusts.Tables(0))
dv.Sort = CType(viewstate.Item("sort"), String)
dg.DataSource = dv
dg.DataBind()
End Sub

Private Sub dg_SortCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridSortCommandEventArgs)
Dim dv As New DataView(dsCusts.Tables(0))
dv.Sort = e.SortExpression
dg.DataSource = dv
dg.DataBind()
'Save the sort criteria for use in the paging sub
viewstate.Add("sort", e.SortExpression)
End Sub

betulus said:
Fair enough. I guess setting the sort order after building the DataView is
not so efficient, but may be not significantly. I guess that also means
storing the DataView at page level scope. I hadn't considered that.
 
C

clintonG

Thanks for your comments Scott. The mud is getting clearer.

<%= Clinton Gallagher
 

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,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top