473,548 Members | 2,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Controls - Page_init order

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="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 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
5 2329
"tshad" <ts**********@f tsolutions.comw rote in message
news:uE******** *****@TK2MSFTNG P05.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
"Mark Rae" <ma**@markNOSPA Mrae.comwrote in message
news:uL******** ******@TK2MSFTN GP03.phx.gbl...
"tshad" <ts**********@f tsolutions.comw rote in message
news:uE******** *****@TK2MSFTNG P05.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
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**********@f tsolutions.comw rote in message
news:ef******** ******@TK2MSFTN GP02.phx.gbl...
"Mark Rae" <ma**@markNOSPA Mrae.comwrote in message news:uL******** ******@TK2MSFTN GP03.phx.gbl...
>"tshad" <ts**********@f tsolutions.comw rote in message
news:uE******* ******@TK2MSFTN GP05.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
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.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**********@f tsolutions.comw rote in message
news:ef******** ******@TK2MSFTN GP02.phx.gbl...
>"Mark Rae" <ma**@markNOSPA Mrae.comwrote in message
news:uL******* *******@TK2MSFT NGP03.phx.gbl.. .
>>"tshad" <ts**********@f tsolutions.comw rote in message
news:uE****** *******@TK2MSFT NGP05.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
"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?



Aug 15 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
17608
by: wASP | last post by:
Hello again to all of you geniuses, I'm having a problem trying to load dynamic controls at the initialization phase. I've read the docs, and I thought I had it figured out: <>-<>-<>-<>-<>-<>-<>-<>-<>-<>-<>-<> <>-<>-<>-<>-<>-<>-<>-<>-<>-<>-<>-<> .NET Framework Class Library Control.Init Event
3
2633
by: Steve Drake | last post by:
All, I have a CONTROL that contains 1 control (Control ONE), the 1 control that it can contain 1 or 2 control (Control A and B). Control A, raises and event and Control ONE receives this event and this causes control B to be created, when this is done the VIEWSTATE is lost for CONTROL B. In the EVENT that causes CONTROL B to be created...
4
6719
by: Chuck Ritzke | last post by:
Hi, I've searched the newsgroup and other sources to understand how to handle runtime controls and see I'm not the only one who's confused, but I'm still not quite sure of the best way to handle from all the various explanations/answers. I'm attempting the typical scenario... I create a variable number of controls at runtime based on...
3
3957
by: Leo J. Hart IV | last post by:
OK, here's another question for the experts: I am building a multi-step (3 steps actually) form using a panel for each step and hiding/displaying the appropriate panel/panels depending on which step you're on. This all works fine, but I ran into some trouble when I started creating controls dynamically in my code-behind file. Each panel...
0
2273
by: jonelling | last post by:
I am having a problem where the page load event is not being fired for certain user controls that I load dynamically in placeholders. Here is what I'm doing in brief, with full test code supplied at the bottom. In my controlling .aspx page, in the page_init() sub, if not postback, I load two user controls and place them in placeholders. ...
10
1479
by: Nathan Sokalski | last post by:
I have a DataList control with an EditTemplate. Three of the controls in this template include a Calendar, a Button with CommandName="update", and a Button with CommandName="cancel". Whenever I click one of these controls, the DataList replaces the EditTemplate with the ItemTemplate. I think this is because the method I wrote which performs...
2
1318
by: BillE | last post by:
I have a web form which has dynamic controls for order entry, (item selected, #,etc.) which are wired to an event handler which fires when the dynamic controls content changes. These controls have to be recreated on the Page_Init for the events to fire. The content of the controls is based on the current CustomerID, and the order data...
12
2444
by: Michael Lang | last post by:
I'm adding checkbox controls to a panel in a post back, I then have a second post back in which I attempt to process the checkbox controls however they seem to have disappeared off the panel. The following code demonstrates what I'm trying to do. Can anyone explain why there is no checkbox control on the panel when btnTwo is clicked? ...
4
51622
Frinavale
by: Frinavale | last post by:
Introduction Sometimes, when developing web applications, we need to be able to dynamically load controls based on user selections. The following article describes a simple scenario where TextBox controls need to be dynamically loaded according to user input. This simple example can be further extended to dynamically load custom web user...
0
7444
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7711
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7467
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7805
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6039
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5085
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3497
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3478
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1054
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.