When is Control.UniqueId created?

Does anyone know when a UniqueId is assigned?

Now I have the code in my Inti page, which is based on UniqueId. However, depending on some kind of business logic, I may need to change the page management hierarchy before this happens.

So my main question is: when is UniqueId assigned? Is it possible for me to change the hierarchy in Page_PreInit () so that when my code fires in Page_Init (), I will have the appropriate UniqueId assigned?

+4
source share
3 answers

To answer this question, I wrote a little code that records the values ​​of the UniqueID property for the control for each of these events:

  • Preinit
  • Init
  • Initcomplete
  • Preload
  • Load
  • Loadcomplete
  • Prerender
  • PreRenderComplete

The last event handler, for example, looked like this:

protected void Page_PreRenderComplete(object sender, EventArgs e) { _uniqueIdValues.Add( new Tuple<string, string>("PreRenderComplete", MyControl.UniqueID)); } 

Then I set a breakpoint in the Unload event and used the Visual Studio Immediate window to print the registered values:

 _uniqueIdValues.ToArray() {System.Tuple<string,string>[8]} [0]: {(PreInit, MyControl)} [1]: {(Init, MyControl)} [2]: {(InitComplete, MyControl)} [3]: {(PreLoad, MyControl)} [4]: {(Load, MyControl)} [5]: {(LoadComplete, MyControl)} [6]: {(PreRender, MyControl)} [7]: {(PreRenderComplete, MyControl)} 

It looks like UniqueID was set to the “MyControl” line (which was actually the ID property that I specified for the control in the ASPX label) for each of the events. It would seem that @Rewinder's answer from MSDN is correct. They are set before any of the events are triggered at the page level of ASP.NET.

EDIT:

If we look at the source source of .NET 3.5 (http://referencesource.microsoft.com/) for System.Web.UI.Control, we see that the return value of UniqueID is calculated when the property is accessed. The UniqueID property looks like this:

 public virtual string UniqueID { get { if (_cachedUniqueID != null) { return _cachedUniqueID; } Control namingContainer = NamingContainer; if (namingContainer != null) { // if the ID is null at this point, we need to have one created and the control added to the // naming container. if (_id == null) { GenerateAutomaticID(); } if (Page == namingContainer) { _cachedUniqueID = _id; } else { string uniqueIDPrefix = namingContainer.GetUniqueIDPrefix(); if (uniqueIDPrefix.Length == 0) { // In this case, it is probably a naming container that is not sited, so we don't want to cache it return _id; } else { _cachedUniqueID = uniqueIDPrefix + _id; } } return _cachedUniqueID; } else { // no naming container return _id; } } } 

And this method is called when the naming container changes. The ClearCachedUniqueIDRecursive method resets the value of the _cachedUniqueID field so that it is restored the next time the UniqueID property is called.

 private void UpdateNamingContainer(Control namingContainer) { // Remove the cached uniqueID if the control already had a namingcontainer // and the namingcontainer is changed. if (_namingContainer != null && _namingContainer != namingContainer) { ClearCachedUniqueIDRecursive(); } _namingContainer = namingContainer; } 
+6
source

Is it possible for me to change the hierarchy in Page_PreInit () so that when my code fires in Page_Init (), I will have the appropriate UniqueId assigned?

I can’t say exactly what you are trying to do, but it looks like Microsoft is discouraging him:

Generally, you do not need to use the UniqueID property. For example, you should not write code that controls links using the predicted value of the generated UniqueID Property. You can read and pass the value of the UniqueID property to other processes, but you should not rely on a specific structure.

+1
source

From MSDN :

This identifier is automatically generated when page requests are processed.

So you have access to uniqueId during PreInit

EDIT

Well, I think I was a little vague. Page.ProcessRequest calls the FrameworkInitialize() method, which will build the control tree. This happens before PreInit, so UniqueId control is available.

0
source

Source: https://habr.com/ru/post/1345711/


All Articles