扩展 ASP.NET Web 控件时,应在什么情况下注入额外的 Web 控件?

发布于 2024-07-08 16:21:26 字数 433 浏览 5 评论 0原文

我试图通过扩展现有控件来避免复合控件或使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

总以为 2024-07-15 16:21:26

实际上,微软说你应该覆盖 CreateChildControls< /a> 方法。

您可以在添加控件之前或之后调用基类方法,我不确定那里是否有约定。

protected override void CreateChildControls(){
  Controls.Add(someControl);
  base.CreateChildControls();
}

希望有帮助!

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.

protected override void CreateChildControls(){
  Controls.Add(someControl);
  base.CreateChildControls();
}

Hope that helps!

欢烬 2024-07-15 16:21:26

您应该将它们添加到 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文