Override Page_PreInit () globally without subclassing Page?

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

+4
source share
3 answers

Another way to do this is to use the 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 be launched on every init page.

+6
source

Why are you trying to avoid inheritance from the page?

Your question is somewhat controversial ... The phrase "override Page_Init" implies an inheritance that you are trying to avoid. Two options:

1) Perform the task in the BeginRequest handler as Simon's suggestions. 2) Create a new HttpHandler for use, not for the page. You can encapsulate the page object, but I don't think you can directly call Page.ProcessRequest, so this might be pointless.

+3
source

You can write your own class, which is derived from System.Web.UI.Page, and there you override the method

 public class CultureBasePage : System.Web.UI.Page { protected overide InitializeCulture(object sender, EventArgs e) { // set thread culture and ui culture here base.InitializeCulture(sender, e); } } 

then set your pages to be pulled out of this class

 public class _Default : CultureBasePage {} 

If you donโ€™t need the code file, you can install it also in the aspx file:

 <%@ Page Language="C#" Inherits="CultureBasePage" %> 
0
source

All Articles