Controls - Page_init order

T

tshad

I have a PageInit.ascx that I want to put in all my pages and have it
execute only once during the "not IsPostback" section. I also need it to
execute first before anything else.

I have it set as:

<%@ Register TagPrefix="fts" TagName="pageInit"
Src="/controls/pageInit.ascx" %>
....
<body>
....
<fts:pageInit runat="server"/>

The whole .ascx file is:
************************************************
<script runat="server">
Sub Page_Load(sender as Object, e as EventArgs)
trace.warn("Inside PageInit.ascx")
Dim UserLoggedOn as Label = CType(Page.FindControl("UserLoggedOn"),Label)
Dim UserLoggedOnLabel as Label =
CType(Page.FindControl("UserLoggedOnLabel"),Label)
if not UserLoggedOn is nothing then
if session("LoggedIn") <> nothing then
if session("firstName") <> nothing then
UserLoggedOn.Text = UserLoggedOn.Text & session("firstName")
if session("lastName") <> nothing then
UserLoggedOn.Text = UserLoggedOn.Text & " " & session("lastName")
end if
end if
if not UserLoggedOn is nothing then
UserLoggedOn.visible = true
if not UserLoggedOnLabel is nothing then UserLoggedOnLabel.visible =
true
end if
end if
end if
if not session("User") is nothing then session("LastPageVisited") =
Session("User").LastPageVisited
End Sub
</script>
**************************************************************

This works fine, except that the Page_Load function doesn't run until after
all the Page_Load code has run.

Is there a way to get my control to execute first?

Thanks,

Tom.
 
J

Juan T. Llibre

re:
This works fine, except that the Page_Load function doesn't run until after all the Page_Load code
has run.

If it means anything, I'm puzzled by your statement, too.

Can you clarify ?
 
T

tshad

Sorry.

Actually, the heading should be Controls - Page_Load order.

I can see where it may be confusing. I rewrote it slightly, so hopefully it
is more clear.

I have a control (PageInit.ascx) that I want to put in all my pages and have
it
execute only once during the "not IsPostback" section. I also need it to
execute first before anything else since it is initialization code. I don't
want it to run during postback. I want to place this controls on all my
pages. This will allow me to add code later on that I want to pertain to
all my pages without having to change all my pages.

I placed trace.warn statements to show me what the order of execution is.

I tried 2 ways and each way the trace.warn statement I put in the control is
the last trace statement I see, so I assume all the other code in the parent
page gets executed first then the Page_Load code from the control
(PageInit.ascx) gets executed. Not what I am trying to do.

1) I have the parent set as:

*********************************************
<%@ Register TagPrefix="fts" TagName="pageInit"
Src="/controls/pageInit.ascx" %>
....
<body>
....
<fts:pageInit runat="server"/>
************************************************

The whole .ascx file is:
************************************************

<script runat="server"> Sub Page_Load(sender as Object, e as EventArgs)
trace.warn("Inside PageInit.ascx") Dim UserLoggedOn as Label =
CType(Page.FindControl("UserLoggedOn"),Label) Dim UserLoggedOnLabel as
Label = CType(Page.FindControl("UserLoggedOnLabel"),Label) if not
UserLoggedOn is nothing then if session("LoggedIn") <> nothing then
if session("firstName") <> nothing then UserLoggedOn.Text =
UserLoggedOn.Text & session("firstName") if session("lastName") <>
nothing then UserLoggedOn.Text = UserLoggedOn.Text & " " &
session("lastName") end if end if if not UserLoggedOn is
nothing then UserLoggedOn.visible = true if not UserLoggedOnLabel
is nothing then UserLoggedOnLabel.visible = true end if end if end
if if not session("User") is nothing then session("LastPageVisited") =
Session("User").LastPageVisited End Sub </script>
**************************************************************

This works fine, except that the Page_Load function (from my control)
doesn't run until after all the Page_Load code has run.

2) I also tried loading the same .ascx file dynamically.

**************************************************
....
Sub Page_Load(sender as Object, e as EventArgs)
Dim sTest as String
trace.warn("UserID = " & session("User").UserID)
if not IsPostBack
Dim pageControl As Control = LoadControl("/controls/pageInit.ascx")
PageUserControl.Controls.Add(pageControl)
....
trace.warn("In Parent 1")
....
end if
trace.warn("In Parent 2")
....
End Sub
....
<body>

<asp:placeholder ID="PageUserControl" runat="server"/>
<form runat="server">
....

*******************************************

My trace will show as:

In Parent 1
In Parent 2
Inside PageInit.ascx

Is there a way to get my control to execute first and only once?

Thanks,

Tom.
 
T

tshad

I did quite a bit of testing by putting trace.warn statements in the code to
try to find what was happening as well as writting to a text file to test it
out.

I just tried to put the code in the Page_Init and found that it still does
the control after the Page_Load (unless this is some quirk with trace.warn).

I even added the trace.warn line at the beginning of the Page_Load of the
Parent that says "Inside the Parent Page_Load", just to make sure there
wasn't a cache problem and I wasn't loading the new page.

The new code now looks essentially like:


<script runat="server"> Sub Page_Init(sender as Object, e as EventArgs)
Dim pageControl As Control = LoadControl("/controls/pageInit.ascx")
PageUserControl.Controls.Add(pageControl) End Sub Sub Page_Load(sender as
Object, e as EventArgs) Dim sTest as String trace.warn("Inside the
Parent Page_Load") trace.warn("UserID = " & session("User").UserID) if
not IsPostBack trace.warn("referer = " &
Request.ServerVariables("HTTP_REFERER")) sTest =
Request.ServerVariables("HTTP_REFERER") trace.warn("before session of
newposition test") ...

Here is the results in the Trace section of the page:

Inside the Parent Page_Load 0.014399 0.004615
UserID = 152 0.014620 0.000222
before session of newposition test 0.014779 0.000022
before request of PositionID test 0.014804 0.000025
getting newPosition 0.014824 0.000020
Checking CompanyID with User 0.020765 0.005941
PositionID = 294 0.025222 0.004457
at SelectRecord 0.040893 0.015671
PositionReader('PositionID') = 294 0.063810 0.022792
Before ApplicantPositionID 0.063852 0.000042
Wages = 0.00-0.00 USD/Yr 0.063944 0.000091
ResumeMyself = Thomas Scheiderich, 0.063971 0.000028
newPosition = Toms' New Postings 0.068356 0.004385
Before clearing ErrorMessage2 - ErrorMessage2 = 0.068397 0.000041
After clearing ErrorMessage2 - ErrorMessage2 = 0.068417 0.000020
Inside PageInit.ascx 0.068446 0.000029

As you can see the PageInit, even though it was in the Page_Init Sub, was
done last.

Just to make sure it wasn't the trace.warn causing a problem I added the
following code in the entry and exit if Page_Init, Page_Load of the Parent
and the control and one in the "Not IsPostBack section:


Dim fs as New FileStream("c:\inetpub\wwwroot\sw\uploads\tom1.txt",
FileMode.Append, FileAccess.Write) Dim s as new StreamWriter(fs)
s.BaseStream.Seek(0,SeekOrigin.End) s.WriteLine("Inside Page_Init.ascx")
s.close()

I got the following results in my text file:

Inside Page_Init.ascx
Exiting Page_Init.ascx
Inside Page_Load.ascx
Inside not IsPostBack - .aspx
Exiting Page_load - .ascx
Inside PageInit.ascx
Exiting PageInit.ascx

So it appears that it does the Page_Init first, then Page_Load, then it must
go through the Controls (including their Page_Load code).

So, unless I am missing something, this makes it useless for any type of
initialization code.

One way around this would be to use an include file, unless the same thing
happens with that.

Thanks,

Tom
 

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

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top