如果我无法在控件中使用 OnPreInit,如何在正确的时间动态添加控件?

发布于 2024-08-16 06:21:36 字数 244 浏览 5 评论 0原文

我想要一个用户控件,它将动态地在 asp.NET 面板中添加一些复选框。

只是我相信我可以在控件的 OnPreInit 方法中轻松做到这一点。但问题是我了解到我无法在控件上使用和重写 OnPreInit 方法;它仅用于页面。

我不想通过调用控件上的方法从页面解决此问题。

那么,如果我无法在控件上使用 PreInit,那么动态添加复选框的正确位置在哪里?

我错过了什么吗?有什么建议吗?

谢谢。

I want to have a user control which will add some checkboxes in a asp.NET panel dynamically.

Simply I was believing I can do that easily in control's OnPreInit method. But the thing is I have learn that I cannot use and override OnPreInit method on Controls; it is only used for pages.

I do not want to solve this from the page by calling a method on the control.

So, if I cannot use PreInit on controls, where is the right place to add my checkboxes dynamically?

Do I miss something? Any advices?

thanks.

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

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

发布评论

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

评论(4

墨洒年华 2024-08-23 06:21:36

我认为很大程度上取决于你想用它们做什么 - 你应该知道动态添加的控件有自己的生命周期(谷歌搜索应该会为你提供大量信息,但本质上它们会“赶上”任何阶段)他们错过了)。我之所以提到这一点,是因为您可能想研究一些陷阱——当我第一次经历它时,我花了几天时间来思考它。

为了回答您的问题,我建议最好的位置是在控件的 OnInit 阶段添加复选框。为了供您参考,我在下面提供了标准页面加载期间事件发生的标准顺序,并在页面上声明了正常的自定义控件。

Page: Constructor

Page: OnPreInit

---- Declared Controls Constructed ----

Control: OnInit

Page: OnInit

Page: OnInitComplete

Page: LoadViewState

Control: LoadViewState

Page: OnPreLoad

Page: OnLoad

Control: OnLoad

---- Any Control Events (e.g. btnClick) ----

Page: OnLoadComplete

Page: OnPreRender

Control: OnPreRender

Page: OnPreRenderComplete

Page: SaveViewState

Control: SaveViewState

Page: OnSaveStateComplete

Page: Render

Page: RenderChildren  -> Control: Render

Control: OnUnload

Page: OnUnload

I think a lot really depends on what you want to do with them - you should be aware that controls added dynamically get their own lifecycle of sorts (googling should get you a ton of information, but essentially they will "catch up" on any phases they missed). I only mention this as you may want to look into some of the pitfalls - when I first went through it I spent a few days getting my head around it.

To answer your question I would suggest the best place will be to add the checkboxes during the OnInit phase of the control. For your information I have provided below the standard order that events occur during a standard page load with a normal custom control declared on the page.

Page: Constructor

Page: OnPreInit

---- Declared Controls Constructed ----

Control: OnInit

Page: OnInit

Page: OnInitComplete

Page: LoadViewState

Control: LoadViewState

Page: OnPreLoad

Page: OnLoad

Control: OnLoad

---- Any Control Events (e.g. btnClick) ----

Page: OnLoadComplete

Page: OnPreRender

Control: OnPreRender

Page: OnPreRenderComplete

Page: SaveViewState

Control: SaveViewState

Page: OnSaveStateComplete

Page: Render

Page: RenderChildren  -> Control: Render

Control: OnUnload

Page: OnUnload
携余温的黄昏 2024-08-23 06:21:36

您也可以在 Page_Load 事件中执行此操作。那有什么问题吗?

You can also do it in Page_Load event. Whats the problem in that?

陈独秀 2024-08-23 06:21:36

如果您尝试使用复选框的值,请尝试使用

 Page.Form.FindControl("ID")

if your trying to use the values of the checkedboxes, try using

 Page.Form.FindControl("ID")
嘿咻 2024-08-23 06:21:36

一般的经验法则是尽快创建自定义控件。在 PreRender 之前,控件的所有事件都会正确发生。如果您在 PreRender 中添加自定义控件,它们不会加载其视图状态或调用事件。

The general rule of thumb is to create custom controls as soon as you possibly can. All of the events for controls will happen properly up until PreRender. If you add custom controls at PreRender they do not get their viewstate loaded or events called.

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