ASP.NET 用户控件继承用户控件生成空引用

发布于 2024-10-04 16:21:52 字数 196 浏览 0 评论 0原文

这一定是一个普遍的问题。

  • 用户控件 A 有一个 TextBox T。
  • 用户控件 B 继承自用户控件 A。
  • 用户控件 B 被放置到页面 P 上。

在页面 P 的 Page_Load 事件期间,如果您尝试访问 TextBox T,您会发现它为空。

有一个优雅的解决方案吗?

This must be a common problem.

  • User control A has a TextBox T.
  • User control B inherits from user control A.
  • User control B is placed onto page P.

During the Page_Load event of page P, if you attempt to access TextBox T, you'll find it's null.

Is there an elegant solution for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

春夜浅 2024-10-11 16:21:52

从 UserControls 继承 UserControls 并不是一个好的策略。父 UserControl 中的元素将不会被渲染 - 这就是您的 TextBox 为 null 的原因。

更好的选择是将 UserControl A 包含在 UserControl B 中 - 这样 UserControl A 的元素可以在其他 UserControl 中重复使用。

看一下关于继承 UserControls 的另一篇文章 - 如何(如果有的话)使 ASP.NET UserControl 从另一个 UserControl 继承?

Inheriting UserControls from UserControls is not a good strategy. Elements in the parent UserControl will not be rendered - this is why your TextBox is null.

A better option would be to have UserControl A contained within UserControl B - this way elements of UserControl A can be reused across your other UserControls.

Have a look at this other post about inheriting UserControls - How (if at all) can you make an ASP.NET UserControl inherit from another UserControl?.

空城仅有旧梦在 2024-10-11 16:21:52

控件 B 的 ASCX 是否包含文本框 T?为了使文本框 T 不为空,需要这样做。如果 controlB 不包含文本框 T,则后面代码中声明的属性不会绑定到任何前端控件,因此保持为 null。

Does control B's ASCX include textbox T? It needs to in order for textbox T to be not null. If controlB doesn't include textbox T, the property declared in the code behind isn't bound to any front end control and thus remains null.

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