By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,626 Members | 2,169 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,626 IT Pros & Developers. It's quick & easy.

Controls - Page_init order

P: n/a
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.
Aug 15 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"tshad" <ts**********@ftsolutions.comwrote in message
news:uE*************@TK2MSFTNGP05.phx.gbl...
This works fine, except that the Page_Load function doesn't run until
after all the Page_Load code has run.
???
Aug 15 '06 #2

P: n/a
"Mark Rae" <ma**@markNOSPAMrae.comwrote in message
news:uL**************@TK2MSFTNGP03.phx.gbl...
"tshad" <ts**********@ftsolutions.comwrote in message
news:uE*************@TK2MSFTNGP05.phx.gbl...
>This works fine, except that the Page_Load function doesn't run until
after all the Page_Load code has run.

???
What?
Aug 15 '06 #3

P: n/a
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.com : 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**********@ftsolutions.comwrote in message
news:ef**************@TK2MSFTNGP02.phx.gbl...
"Mark Rae" <ma**@markNOSPAMrae.comwrote in message news:uL**************@TK2MSFTNGP03.phx.gbl...
>"tshad" <ts**********@ftsolutions.comwrote in message
news:uE*************@TK2MSFTNGP05.phx.gbl...
>>This works fine, except that the Page_Load function doesn't run until after all the Page_Load
code has run.

???
What?

Aug 15 '06 #4

P: n/a
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.
"Juan T. Llibre" <no***********@nowhere.comwrote in message
news:eQ**************@TK2MSFTNGP04.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.com : 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**********@ftsolutions.comwrote in message
news:ef**************@TK2MSFTNGP02.phx.gbl...
>"Mark Rae" <ma**@markNOSPAMrae.comwrote in message
news:uL**************@TK2MSFTNGP03.phx.gbl...
>>"tshad" <ts**********@ftsolutions.comwrote in message
news:uE*************@TK2MSFTNGP05.phx.gbl...

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

???
What?


Aug 15 '06 #5

P: n/a
"tshad" <ts**********@ftsolutions.comwrote in message
news:%2***************@TK2MSFTNGP05.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"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
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.
"Juan T. Llibre" <no***********@nowhere.comwrote in message
news:eQ**************@TK2MSFTNGP04.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.com : 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**********@ftsolutions.comwrote in message
news:ef**************@TK2MSFTNGP02.phx.gbl...
>>"Mark Rae" <ma**@markNOSPAMrae.comwrote in message
news:uL**************@TK2MSFTNGP03.phx.gbl...
"tshad" <ts**********@ftsolutions.comwrote in message
news:uE*************@TK2MSFTNGP05.phx.gbl...

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

???
What?



Aug 15 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.