开发者

CreateChildControls method ChildControls Do not Fire Events

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 Contr开发者_高级运维ols.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


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜