首页 新闻 会员 周边 捐助

关于自定义控件的问题.

0
悬赏园豆:100 [已关闭问题] 关闭于 2011-04-06 22:06

按照MSDN的说法,页面上每一个实现了IPostBackDataHandler接口的控件在回传时都会执行自己的LoadPostData方法来更新自己的状态(参见“处理回发数据”),前提是要在自定义控件内添加一个“Name”属性和自定义控件的UniqueId相同的元素.

我对这个前提比较感兴趣.想找一下为什么要这个前提:于是源代码发现

private void ProcessPostData(NameValueCollection postData, bool fBeforeLoad)

 foreach (string str in postData)

if ((postBackDataHandler != null) && postBackDataHandler.LoadPostData(str, this._requestValueCollection))

我发现  postData 和 _requestValueCollection 值是相等的.

那为什么在具体控件中调用

 bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)

这个方法时发现如果你不按照上面的做法(在自定义控件内添加一个“Name”属性和自定义控件的UniqueId相同的元素.),

postDataKey 与 postCollection 的 Key 不相同呢.

问题补充: 自己又研究了一下,找到了答案: foreach (string str2 in this._controlsRequiringPostBack) { Control control2 = this.FindControl(str2); if (control2 != null) { IPostBackDataHandler handler2 = control2._adapter as IPostBackDataHandler; if (handler2 == null) { handler2 = control2 as IPostBackDataHandler; } if (handler2 == null) { throw new HttpException(SR.GetString("Postback_ctrl_not_found", new object[] { str2 })); } if (handler2.LoadPostData(str2, this._requestValueCollection)) { this._changedPostDataConsumers.Add(control2); } continue; } } 这时候调用是通过这种方式调用的。此时_controlsRequiringPostBack集合中的值就是 自定义控件的uniqueID. 而 ._requestValueCollection 还是Request.Form中input的key.所以他们不相同。
kasafuma的主页 kasafuma | 初学一级 | 园豆:200
提问于:2011-03-25 16:48
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册