Custom server control running code before page_load?

  • Thread starter Thread starter Daves
  • Start date Start date
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...
 
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
 
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 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
 
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.
 
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...?
 
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
 
Back
Top