Custom server control running code before page_load?

D

Daves

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...
 
B

Brock Allen

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?
 
S

sonic

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
 
D

Daves

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?
 
B

Brock Allen

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.
 
D

Daves

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.
 
D

Daves

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...?
 
G

garethdjames

Use the pre render event but do it on a page that all other pages
inherit from
 
B

Brock Allen

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; }
}
}
 

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

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top