473,385 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Custom server control running code before page_load?

Is there any way to have a custom server control for all aspx pages which
runs code before the page_load event? I need some code to be run before the
page_load since it's supposed to change some Page properties, something like
an include file in the old classic asp days...
Nov 19 '05 #1
10 1950
There's always the Page_Init event. Or do you need this code to run for every
page? What exactly are you truing to do?

-Brock
DevelopMentor
http://staff.develop.com/ballen
Is there any way to have a custom server control for all aspx pages
which runs code before the page_load event? I need some code to be run
before the page_load since it's supposed to change some Page
properties, something like an include file in the old classic asp
days...


Nov 19 '05 #2
instead you should think about putting your logic in pre-render event
instead of page_load event.

this is a common problem you are experiencing, often encountered when
wiring up button click events and discovering that they execute after
page_load.
So the best thing to do would be to move your main logic to pre-render

Nov 19 '05 #3
I agree with Sonic on this one.
The Pre-Render event is a better place to put your code if you need to
examine state after the control events have fired.
Here's more info:
http://msdn.microsoft.com/library/de...nlifecycle.asp
http://www.15seconds.com/issue/020102.htm
http://msdn.microsoft.com/library/de...singStages.asp

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net

"sonic" <so*******@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
instead you should think about putting your logic in pre-render event
instead of page_load event.

this is a common problem you are experiencing, often encountered when
wiring up button click events and discovering that they execute after
page_load.
So the best thing to do would be to move your main logic to pre-render

Nov 19 '05 #4
well Brock has it - I want to do it on *every* aspx page in application and
so how would I load the user control and run it's code in Pre-Render?

"Steve C. Orr [MVP, MCSD]" <St***@Orr.net> wrote in message
news:eJ**************@TK2MSFTNGP09.phx.gbl...
I agree with Sonic on this one.
The Pre-Render event is a better place to put your code if you need to
examine state after the control events have fired.
Here's more info:
http://msdn.microsoft.com/library/de...nlifecycle.asp
http://www.15seconds.com/issue/020102.htm
http://msdn.microsoft.com/library/de...singStages.asp

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net

"sonic" <so*******@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
instead you should think about putting your logic in pre-render event
instead of page_load event.

this is a common problem you are experiencing, often encountered when
wiring up button click events and discovering that they execute after
page_load.
So the best thing to do would be to move your main logic to pre-render


Nov 19 '05 #5
> well Brock has it - I want to do it on *every* aspx page in
application and so how would I load the user control and run it's code
in Pre-Render?


Well, this still doesn't answer exactly what you'd like the result to be.
You said that you want something to run before Page_Load (for every page)
to set some "default" properties, or somesuch. What are those default properties?
You can always handle preprocessing events in global.asax. This allows you
to write code in one place that gets executed prior to every page request.
But to help with more specific details, I'd like to know the specifics of
what properties you're trying to set.

-Brock
DevelopMentor
http://staff.develop.com/ballen

Nov 19 '05 #6
thx Brock, I'll try to be specific. For example there are AccessDataSources
on the pages and I need to set their DataFile property dynamically as they
are not always the same. I would then iterate the collection and search for
DataSource controls and set this value.
I thought I wouldn't be able to access the current Page class from
global.aspx? Therefor thought I'd do a @Register in the Page for an user
control which runs in Pre-Render event and does this.

"Brock Allen" <ba****@NOSPAMdevelop.com> wrote in message
news:70**********************@msnews.microsoft.com ...
well Brock has it - I want to do it on *every* aspx page in
application and so how would I load the user control and run it's code
in Pre-Render?


Well, this still doesn't answer exactly what you'd like the result to be.
You said that you want something to run before Page_Load (for every page)
to set some "default" properties, or somesuch. What are those default
properties? You can always handle preprocessing events in global.asax.
This allows you to write code in one place that gets executed prior to
every page request. But to help with more specific details, I'd like to
know the specifics of what properties you're trying to set.

-Brock
DevelopMentor
http://staff.develop.com/ballen

Nov 19 '05 #7
or maybe I could override the AccessDataSource constructor with the DataFile
property set to my filepath? So that all instances created in Page have it
set...?
"Daves" <db****@simnet.is> wrote in message
news:eF**************@tk2msftngp13.phx.gbl...
thx Brock, I'll try to be specific. For example there are
AccessDataSources on the pages and I need to set their DataFile property
dynamically as they are not always the same. I would then iterate the
collection and search for DataSource controls and set this value.
I thought I wouldn't be able to access the current Page class from
global.aspx? Therefor thought I'd do a @Register in the Page for an user
control which runs in Pre-Render event and does this.

"Brock Allen" <ba****@NOSPAMdevelop.com> wrote in message
news:70**********************@msnews.microsoft.com ...
well Brock has it - I want to do it on *every* aspx page in
application and so how would I load the user control and run it's code
in Pre-Render?


Well, this still doesn't answer exactly what you'd like the result to be.
You said that you want something to run before Page_Load (for every page)
to set some "default" properties, or somesuch. What are those default
properties? You can always handle preprocessing events in global.asax.
This allows you to write code in one place that gets executed prior to
every page request. But to help with more specific details, I'd like to
know the specifics of what properties you're trying to set.

-Brock
DevelopMentor
http://staff.develop.com/ballen


Nov 19 '05 #8
Use the pre render event but do it on a page that all other pages
inherit from

Nov 19 '05 #9
You can access every page from within global.asax. You'll have to access
HttpContext.Current.Handler and cast it to a Page.

As for your approach, I'd suggest doing an expression builder instead. I
think it will do what you want quite well:

http://beta.asp.net/QUICKSTART/aspne...bility.aspx#EB

-Brock
DevelopMentor
http://staff.develop.com/ballen
thx Brock, I'll try to be specific. For example there are
AccessDataSources
on the pages and I need to set their DataFile property dynamically as
they
are not always the same. I would then iterate the collection and
search for
DataSource controls and set this value.
I thought I wouldn't be able to access the current Page class from
global.aspx? Therefor thought I'd do a @Register in the Page for an
user
control which runs in Pre-Render event and does this.
"Brock Allen" <ba****@NOSPAMdevelop.com> wrote in message
news:70**********************@msnews.microsoft.com ...
well Brock has it - I want to do it on *every* aspx page in
application and so how would I load the user control and run it's
code in Pre-Render?

Well, this still doesn't answer exactly what you'd like the result to
be. You said that you want something to run before Page_Load (for
every page) to set some "default" properties, or somesuch. What are
those default properties? You can always handle preprocessing events
in global.asax. This allows you to write code in one place that gets
executed prior to every page request. But to help with more specific
details, I'd like to know the specifics of what properties you're
trying to set.

-Brock
DevelopMentor
http://staff.develop.com/ballen


Nov 19 '05 #10
And I just realized the sample I sent you to doesn't have any meaningful
implementation, so here's a quick and dirty one that reads from the appSettings.
You'd modify this to get your own data:

public class MyAppSettingsExpression : ExpressionBuilder
{
public static object GetSetting(string param)
{
return ConfigurationManager.AppSettings[param];
}
public static object GetSetting(string param, Type propType, string propName)
{
return ConfigurationManager.AppSettings[param];
}

public override System.CodeDom.CodeExpression GetCodeExpression(
BoundPropertyEntry entry,
object parsedData,
ExpressionBuilderContext context)
{
if ((entry.DeclaringType == null) || (entry.PropertyInfo == null))
{
CodeExpression[] expressionArray1 =
new CodeExpression[1] { new CodePrimitiveExpression(entry.Expression.Trim())
};
return new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(this.GetType()),
"GetSetting", expressionArray1);
}
CodeExpression[] expressionArray2 = new CodeExpression[3] { new CodePrimitiveExpression(entry.Expression.Trim()),
new CodeTypeOfExpression(entry.DeclaringType), new CodePrimitiveExpression(entry.PropertyInfo.Name)
};
return new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(this.GetType()),
"GetSetting", expressionArray2);
}
public override object EvaluateExpression(object target, BoundPropertyEntry
entry, object parsedData, ExpressionBuilderContext context)
{
return GetSetting(entry.Expression, target.GetType(), entry.PropertyInfo.Name);
}
public override bool SupportsEvaluate
{
get { return true; }
}
}

-Brock
DevelopMentor
http://staff.develop.com/ballen


Nov 19 '05 #11

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

Similar topics

0
by: Santa | last post by:
I am using Fritz Onion's "Asynchronous Pages" approach as mentioned in the article http://msdn.microsoft.com/msdnmag/issues/03/06/Threading/default.aspx to increase the performance of my ASPX...
0
by: adam | last post by:
i have custom user control and i'm trying to pass values to custom user control......I need help it seems to me i cannot pass the value to user control from dropdownlist. I have property in a...
2
by: Jay Walker | last post by:
I created a custom DataGridColumn based on Marcie Robillard's MSDN Article: Creating Custom Columns for the ASP.NET Datagrid...
3
by: The Developer | last post by:
Hi All, I have a web application where I am adding a custom attribute to my ASP.NET text box control and changing value of that attribute at client side using JavaScript. My problem is that...
7
by: Shimon Sim | last post by:
I have a custom composite control I have following property
9
by: Jaybuffet | last post by:
my aspx has something like this <asp:Repeater id="Repeater1" runat="server"> <ItemTemplate> <mycontrol:ctl id="ctlId" obj='<%# Container.DataItem %>' showItem="true"/> </ItemTemplate>...
0
by: OceanBreeze | last post by:
I have added a LinkButton to a table cell programmatically inside the Page_Load method. I also added a custom event to that link button. The same custom event is valid for all the link buttons. The...
2
by: Michal Valent | last post by:
I would like to fire some custom server control event before Page_Load event like this (from the trace of an aspx page) : Trace Information Category Message From First(s) From Last(s) aspx.page...
6
by: tshad | last post by:
I was looking at a page that showed how to set up a custom event and it seems to work ok. But I am not sure how I would use it. How would I subscribe to it. There is actual action (such as...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.