I think that someday I saw an example about this, but I can no longer find it:
Was there a way to override the Page_Init () event globally without creating a new MyCustomPage class inherited from the page?
I was hoping there was some way to make global overrides on a page without subclassing the page (without having to inherit my pages from a subclass). I was thinking of something about Global.asax strings, but for the page.
This is the code that I want to run in PreInit on every page:
'Change Culture 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:
To date, there are 2 excellent solutions (for my case), which are offered below. I chose one of them as the accepted answer, even if I use a different solution. This is because the one I will use was suggested as a comment by @Simon on this question.
The solution was to put 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 subclassing. This seemed to cause some confusion. The reason is that I am creating a project template with many typically required functions already in place (as User Admin, Rights Management, etc.).
For projects created using this template, I want to use es-MX Culture by default and change the ShortDatePattern to display the months in the names of the abbreviated months. Since this is a project template, I want my programmers not to need to implement their pages inherited from the user class. It just seems โstrangeโ to me to subclass pages on this (project template).
Another solution. The one I designated as accepted was provided by @Ishtar. I have already implemented it and it works great and fits my needs. The only thing missing in the code example below is the call to MyBase.OnInit (e).
I want to thank everyone who suggested solutions and comments, and very special thanks to @Ishtar, who gave the initial answer and stayed with this question, since I rejected it for my needs. He then provided a second answer, which turned out to be right (the one that was marked as accepted).