"tshad" <ts**********@f tsolutions.comw rote in message
news:%2******** *******@TK2MSFT NGP05.phx.gbl.. .
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"S ub Page_Init(sende r as Object, e as EventArgs)
Dim pageControl As Control = LoadControl("/controls/pageInit.ascx")
PageUserControl .Controls.Add(p ageControl) End Sub Sub Page_Load(sende r as
Object, e as EventArgs) Dim sTest as String trace.warn("Ins ide the
Parent Page_Load") trace.warn("Use rID = " & session("User") .UserID) if
not IsPostBack trace.warn("ref erer = " &
Request.ServerV ariables("HTTP_ REFERER")) sTest =
Request.ServerV ariables("HTTP_ REFERER") trace.warn("bef ore 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 ApplicantPositi onID 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\tom 1.txt",
FileMode.Append , FileAccess.Writ e) Dim s as new StreamWriter(fs )
s.BaseStream.Se ek(0,SeekOrigin .End) s.WriteLine("In side 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
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="pageIn it"
Src="/controls/pageInit.ascx" %>
...
<body>
...
<fts:pageInit runat="server"/>
*************** *************** *************** ***
The whole .ascx file is:
*************** *************** *************** ***
<script runat="server" Sub Page_Load(sende r as Object, e as EventArgs)
trace.warn("Ins ide PageInit.ascx") Dim UserLoggedOn as Label =
CType(Page.Find Control("UserLo ggedOn"),Label) Dim UserLoggedOnLab el as
Label = CType(Page.Find Control("UserLo ggedOnLabel"),L abel) if not
UserLoggedOn is nothing then if session("Logged In") <nothing then if
session("firstN ame") <nothing then UserLoggedOn.Te xt =
UserLoggedOn.Te xt & session("firstN ame") if session("lastNa me") <>
nothing then UserLoggedOn.Te xt = UserLoggedOn.Te xt & " " &
session("lastNa me") end if end if if not UserLoggedOn is
nothing then UserLoggedOn.vi sible = true if not
UserLoggedOnLab el is nothing then UserLoggedOnLab el.visible = true
end if end if end if if not session("User") is nothing then
session("LastPa geVisited") = Session("User") .LastPageVisite d 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(sende r as Object, e as EventArgs)
Dim sTest as String
trace.warn("Use rID = " & session("User") .UserID)
if not IsPostBack
Dim pageControl As Control = LoadControl("/controls/pageInit.ascx")
PageUserControl .Controls.Add(p ageControl)
...
trace.warn("In Parent 1")
...
end if
trace.warn("In Parent 2")
...
End Sub
...
<body>
<asp:placeholde r ID="PageUserCon trol" 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.
"Juan T. Llibre" <no***********@ nowhere.comwrot e in message
news:eQ******** ******@TK2MSFTN GP04.phx.gbl...
>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 ?
Juan T. Llibre, asp.net MVP
aspnetfaq.co m : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
============== =============== ======
"tshad" <ts**********@f tsolutions.comw rote in message
news:ef******* *******@TK2MSFT NGP02.phx.gbl.. .
>>"Mark Rae" <ma**@markNOSPA Mrae.comwrote in message
news:uL****** ********@TK2MSF TNGP03.phx.gbl. ..
"tshad" <ts**********@f tsolutions.comw rote in message
news:uE***** ********@TK2MSF TNGP05.phx.gbl. ..
This works fine, except that the Page_Load function doesn't run until
after all the Page_Load code has run.
???
What?