I think somewere I saw an example about this but can not find it anymore:
Is there was a way to override a Page_Init() event on a global basis without creating a new MyCustomPage class inherited from Page?
I was in the hope that there were some way to make global overrides to Page without subclassing Page (without the need to inherith my pages from the subclass). I was thinking about something about the lines of Global.asax, but for Page.
This is the code I want to be run at PreInit in every page:
' Change Culture's ShortDate to "dd/mmm/yyyy" for es-MX. If CultureInfo.CurrentCulture.Name = "es-MX" Then Dim info As CultureInfo = CultureInfo.CurrentCulture.Clone info.DateTimeFormat.ShortDatePattern = "dd/MMM/yyyy" System.Threading.Thread.CurrentThread.CurrentCulture = info info = Nothing End If
Thanks in advance for your time.
Edit:
As of today, there have been 2 perfectly good solutions (for my case) offered below. I have selected one of those as accepted answer even I will be currently using the other solution. This is because the one I will be using was offered as a comment by @Simon to this question.
The solution was to place my code in Global.asax as:
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs) ' Configure ShortDatePattern as "dd/MMM/yyyy" for es-MX to avoid month-day confusions when both are numeric. If CultureInfo.CurrentCulture.Name = "es-MX" Then Dim oCultureInfo As CultureInfo = CultureInfo.CurrentCulture.Clone oCultureInfo.DateTimeFormat.ShortDatePattern = "dd/MMM/yyyy" System.Threading.Thread.CurrentThread.CurrentCulture = oCultureInfo oCultureInfo = Nothing End If End Sub
In my question I asked how to override Page_Init() without sub-classing Page. This seemed to cause some confusion. The reason is I am creating a project template with lot's of typically required functionality already in place (as User Admin, Rights Management, etc.).
For projects created with this template, I want to default to es-MX Culture and to modify ShortDatePattern to display months in abbreviated month names. As this is a project template I want to save my programmers from the need to implement their pages inheriting from a custom class. It just feels "weird" to me to imposing sub-classing for pages at this (project template) level.
The other solution. The one I marked as accepted was provided by @Ishtar. I have already implemented it and it works perfectly and falls within my needs. The only thing missing in the example code he provided is the need to call MyBase.OnInit(e).
I want to thank everyone who offered solutions and comments and a very special thank you to @Ishtar who provided an initial answer and stayed with this subject as I rejected it for my needs. Then he provided a second answer who proved right (the one marked as accepted).
Another one way to do this is using Page Adapter
public class TestPageAdapter : System.Web.UI.Adapters.PageAdapter
{
protected override void OnInit(EventArgs e)
{
if (Thread.CurrentThread.CurrentCulture.Name == "es-MX")
{
CultureInfo info = (CultureInfo)Thread.CurrentThread.CurrentCulture.Clone();
info.DateTimeFormat.ShortDatePattern = "dd/MMM/yyyy";
System.Threading.Thread.CurrentThread.CurrentCulture = info;
info = null;
}
}
}
then register your PageAdapter class in App_Browsers/all.browser
<browsers>
<browser refID="Default">
<controlAdapters>
<adapter controlType="System.Web.UI.Page" adapterType="TestPageAdapter" />
</controlAdapters>
</browser>
</browsers>
it will runn on every page init.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With