CreateChildControls 方法 ChildControls 不触发事件

发布于 2024-10-24 17:26:09 字数 487 浏览 0 评论 0原文

我创建了一个自定义 Web 控件。在 CreateChildControls 方法的该控件中,我动态创建一个表,向其中添加包含 ImageButton 的行,并且我试图将一个事件附加到此 ImageButton 但该事件永远不会触发???

我也尝试过 Init 方法,但它不起作用。 CreateChildControls 上的代码如下所示,

  1. 我创建了一个 Panel 。
  2. 我创建一个包含数据和图像按钮的表,
  3. 我将此表添加到创建的面板中
  4. ,我将此面板添加到 Controls.Add 方法中。

我还观察到,如果我直接将控件添加到 Controls.Add() 方法并附加一个事件,它就可以正常工作。

意义 , 1. 创建一个按钮并附加一个事件处理程序 2. 添加到 Controls.Add() 方法

执行上述操作后效果很好。只有子控件事件不会被触发,我缺少什么吗???

提前致谢

I have created a custom web control. In that control on CreateChildControls method i dynamically create a Table add rows to it which contains a ImageButton and i am trying to attach an event to this ImageButton but the event never fires ???

I tried on Init method too it does not work . The code goes like this on CreateChildControls

  1. I create a Panel .
  2. The i create a table with data and an imagebutton on it
  3. I add this table to the created panel
  4. I add this Panel to the Controls.Add method.

I have also observed that if i add a control directly to the Controls.Add() method and attach an event it works fine .

Meaning ,
1. Create a button attach an event handler
2. Add to the Controls.Add() method

On doing the above it works fine . Only the child controls events do not get fired is there anything that i am missing ???

Thanks in advance

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

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

发布评论

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

评论(1

黑白记忆 2024-10-31 17:26:09

我想我和你有同样的问题,但我非常接近解决方案,我只是不知道这是否是最好的解决方案。

对我来说问题是时机。自定义控件及其重写的 CreatChildControls 方法将添加到 Web 部件本身的 CreateChildControls 中的页面控件中。在回发和非回发中,直到 Web 部件的 OnPreRender 方法触发后,才会调用自定义控件的 CCC。

正如您在页面生命周期中看到的,回发事件在Load 和 Render,如果事件没有添加到控件,或者在 Load 结束之前控件没有添加到页面,则事件不会触发。

因此,为了解决这个问题,我们需要以某种方式强制在 Load 结束之前调用自定义控件的 CreateChildControls 方法,就像在 Web 部件的 CCC 期间一样。您无法手动调用该方法:该方法受到保护,无法从外部访问,因此我正在寻找正确的方法来执行此操作。

为了进行测试,我重命名了自定义控件的 CCC,使其成为常规公共方法,并从 Web 部件的 CCC 调用该方法。现在我的事件正常触发了。

如果这对您不起作用,您可以发布您的代码以便我们找到故障吗?

I think I'm having the same problem as you, but I'm pretty close to a solution, I just don't know if it's the best solution.

The problem for me is timing. The custom control with it's overrided CreatChildControls method is added to the pagecontrols in the CreateChildControls of the webpart itself. The CCC of the custom control does not get called until after the OnPreRender method of the webpart fires, both in postback and in non-postback.

As you can see in the page life cycle, postback events are fired between Load and Render, and if the event is not added to the control, or the control is not added to the page before the end of Load, the event does not fire.

So to solve this we need to somehow force the CreateChildControls method of the custom control to be called before end of Load, like during the CCC of the webpart. You cannot call the method manually: the method is protected and cannot be accessed from outside, so I'm looking for the proper way to do this.

For testing I renamed the CCC of the custom control to make it a regular public method, and called that method from the CCC of the webpart. Now my event fires normally.

If this does not work for you, can you please post your code so we can find the fault?

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