扩展 ASP.NET Web 控件时,应在什么情况下注入额外的 Web 控件?
我试图通过扩展现有控件来避免复合控件或使用 ASCX。 但是,我无法将控件添加到继承的控件并保持其视图状态/回发完整性。 每当我在预渲染期间添加控件时,控件都会显示,但回发会引发视图状态异常。 我尝试在此处和 LoadViewState 期间添加它们(这当然是一个不太愚蠢的做法)。 Init 在我扩展的控件中不可用。
例外情况是 Sys.WebForms.PageRequestManagerServerErrorException: 无法加载视图状态。 控制 视图状态所在的树 加载的必须与控制树匹配 用于在期间保存视图状态 之前的请求。 例如, 动态添加控件时, 回发期间添加的控件必须 匹配的类型和位置 在初始阶段添加的控件 请求
I'm trying to avoid a composite control or using and ASCX by extending an existing control. However, I'm having trouble with getting the controls added to the inherited control and keep their view-state/post-back integrity. Whenever I add the controls during pre-render, the controls show up, but the post-back throws a viewstate exception. I tried adding them both there and during LoadViewState (which of course was a long-shot silly). Init is not available from the control which I'm extending.
The exception is
Sys.WebForms.PageRequestManagerServerErrorException:
Failed to load viewsstate. The control
tree into which viewstate is being
loaded must match the control tree
that was used to save viewstate during
the previous request. For example,
when adding controls dynamically, the
controls added during a post-back must
match the type and position of the
controls added during the initial
request
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上,微软说你应该覆盖 CreateChildControls< /a> 方法。
您可以在添加控件之前或之后调用基类方法,我不确定那里是否有约定。
希望有帮助!
Actually, microsoft says you should override the CreateChildControls method.
You can call the base class method before or after you add the controls, I'm not sure if there is a convention there.
Hope that helps!
您应该将它们添加到 OnInit 或 CreateChildControls 中。 无论如何,为了避免 ViewState 出现问题,请阅读 这篇很棒的文章。 示例“4.以编程方式初始化子控件”可能就是您的情况。
You should add them in OnInit or in CreateChildControls. Anyway, to avoid having troubles with ViewState, read this GREAT article. Possibly, sample "4. Initializing child controls programmatically" is your case.