Asp.net TextBox的TextChanged事件使用介绍(3)


if (this._controlsRequiringPostBack != null)
{
this._controlsRequiringPostBack.Remove(str);
}


该方法的第二部分是遍历controlsRequiringPostBack中的集合,它的处理方式和上面一部分类似,只是没有找到控件的id则记录下来

复制代码 代码如下:


else if (fBeforeLoad)
{
if (list == null)
{
list = new ArrayList();
}
list.Add(str2);
}


默认情况下_controlsRequiringPostBack是包含动态创建的控件。这里我们也说说这个集合吧,

controlsRequiringPostBack的设置是在LoadAllState方法中的这一句代码:

this._controlsRequiringPostBack = (ArrayList) first["__ControlsRequirePostBackKey__"];有LoadAllState(加载数据状态)就有SaveAllState(保存数据状态),在SaveAllState中有这么一句代码:

dictionary.Add("__ControlsRequirePostBackKey__", this._registeredControlsThatRequirePostBack);

其中_registeredControlsThatRequirePostBack集合定义在RegisterRequiresPostBack方法中。

复制代码 代码如下:


public void RegisterRequiresPostBack(Control control)
{
if (!(control is IPostBackDataHandler) && !(control.AdapterInternal is IPostBackDataHandler))
{
throw new HttpException(SR.GetString("Ctrl_not_data_handler"));
}
if (this._registeredControlsThatRequirePostBack == null)
{
this._registeredControlsThatRequirePostBack = new ArrayList();
}
this._registeredControlsThatRequirePostBack.Add(control.UniqueID);
}


总之在这里动态添加的控件是没办法加载数据的,但是其它默认的控件在这里都可以处理。

现在我们来看看控件是如何添加的,在Control类中有一个AddedControl方法是真正添加控件的处理:

复制代码 代码如下:


protected internal virtual void AddedControl(Control control, int index)
{
if (control.OwnerControl != null)
{
throw new InvalidOperationException(SR.GetString("Substitution_NotAllowed"));
}
if (control._parent != null)
{
control._parent.Controls.Remove(control);
}
control._parent = this;
control._page = this.Page;
control.flags.Clear(0x20000);
Control namingContainer = this.flags[0x80] ? this : this._namingContainer;
if (namingContainer != null)
{
control.UpdateNamingContainer(namingContainer);
if ((control._id == null) && !control.flags[0x40])
{
control.GenerateAutomaticID();
}
else if ((control._id != null) || (control._controls != null))
{
namingContainer.DirtyNameTable();
}
}
if (this._controlState >= ControlState.ChildrenInitialized)
{
control.InitRecursive(namingContainer);
if (((control._controlState >= ControlState.Initialized) && (control.RareFields != null)) && control.RareFields.RequiredControlState)
{
this.Page.RegisterRequiresControlState(control);
}
if (this._controlState >= ControlState.ViewStateLoaded)
{
object savedState = null;
if ((this._occasionalFields != null) && (this._occasionalFields.ControlsViewState != null))
{
savedState = this._occasionalFields.ControlsViewState[index];
if (this.LoadViewStateByID)
{
control.EnsureID();
savedState = this._occasionalFields.ControlsViewState[control.ID];
this._occasionalFields.ControlsViewState.Remove(control.ID);
}
else
{
savedState = this._occasionalFields.ControlsViewState[index];
this._occasionalFields.ControlsViewState.Remove(index);
}
}
control.LoadViewStateRecursive(savedState);
if (this._controlState >= ControlState.Loaded)
{
control.LoadRecursive();
if (this._controlState >= ControlState.PreRendered)
{
control.PreRenderRecursiveInternal();
}
}
}
}
}


在这个方法中有调用 this.Page.RegisterRequiresControlState(control) 和  control.LoadViewStateRecursive(savedState)方法,一个负责ControlState一个负责 ViewState的数据加载,当我们这里第2次和3次post请求时,在load创建textboxt控件就会加载它已有的控件状态和视图状态。
现在我们再来看看ProcessRequestMain中处理IPostBackEventHandler的那段带代码:

复制代码 代码如下:


this.LoadRecursive();
if (context.TraceIsEnabled)
{
this.Trace.Write("aspx.page", "End Load");
}
if (this.IsPostBack)
{
if (context.TraceIsEnabled)
{
this.Trace.Write("aspx.page", "Begin ProcessPostData Second Try");
}
this.ProcessPostData(this._leftoverPostData, false);
if (context.TraceIsEnabled)
{
this.Trace.Write("aspx.page", "End ProcessPostData Second Try");
this.Trace.Write("aspx.page", "Begin Raise ChangedEvents");
}
this.RaiseChangedEvents();
if (context.TraceIsEnabled)
{
this.Trace.Write("aspx.page", "End Raise ChangedEvents");
this.Trace.Write("aspx.page", "Begin Raise PostBackEvent");
}
this.RaisePostBackEvent(this._requestValueCollection);
if (context.TraceIsEnabled)
{
this.Trace.Write("aspx.page", "End Raise PostBackEvent");
}
}
if (context.TraceIsEnabled)
{
this.Trace.Write("aspx.page", "Begin LoadComplete");
}
this.OnLoadComplete(EventArgs.Empty);


首先我们来看看_leftoverPostData集合是什么,它是在先前一次调用ProcessPostData方法时没有找到控件的一个id集合。在这里就可以找到该控件,执行路线主要就是 ArrayList list = null这句后面部分,最终还是要调用

复制代码 代码如下:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjdfjw.html