Jim*_*m B 4 c# asp.net page-lifecycle .net-3.5 unique-id
有人知道控件的UniqueId何时被分配?
现在我的Page_Init中有一些基于UniqueId的代码.但是,根据某些业务逻辑,我可能需要在发生这种情况之前重新排列页面的控件层次结构.
所以,我的主要问题是,什么时候分配了UniqueId?我是否有可能重新排列Page_PreInit()中的层次结构,以便当我的代码在Page_Init()中触发时,我将分配适当的UniqueId?
为了回答这个问题,我编写了一个小代码隐藏,它记录了每个事件的控件的UniqueID属性的值:
例如,最后一个事件处理程序如下所示:
protected void Page_PreRenderComplete(object sender, EventArgs e)
{
_uniqueIdValues.Add(
new Tuple<string, string>("PreRenderComplete", MyControl.UniqueID));
}
Run Code Online (Sandbox Code Playgroud)
然后,我在Unload事件中设置了一个断点,并使用Visual Studio的立即窗口打印出记录的值:
_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)}
Run Code Online (Sandbox Code Playgroud)
对于每个事件,似乎将UniqueID设置为字符串"MyControl"(实际上是我为ASPX标记中的控件指定的ID属性).似乎@Rewinder的答案来自MSDN是正确的.这些是在触发任何ASP.NET页面级事件之前设置的.
编辑:
如果我们查看System.Web.UI.Control的.NET 3.5参考源(http://referencesource.microsoft.com/),我们可以看到在访问该属性时计算了UniqueID的返回值.UniqueID属性如下所示:
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
并且,当命名容器更改时,将调用以下方法.ClearCachedUniqueIDRecursive方法重置_cachedUniqueID字段的值,以便在下次调用UniqueID属性时重新生成它.
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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2373 次 |
| 最近记录: |