I have just been active on two questions regarding dynamic controls, the answer is nearly always about re-creating on postback, the question is where is the correct place for th开发者_开发技巧is
Here are the two SO questions
question one
question 2
We always do our dynamic control creation by overriding CreateChildControls
Having looked through a lot of controls in reflector, mainly telerik controls i always thought this was correct, but so many people say they should be created in Init.
Should i continue using CreateChildControls or is Init the correct way... Why does this seem such an area of confusion..
In truth, they both accomplish the same thing. If you are developing controls for your organization, then putting it in the init will do just fine (as in it will work even though its "technically the place where it should be", but MS has created a method where they expect it to go.
If you are like a Telerik, where you are creating controls someone else will use, then I would definitely put it in the CreateChildControls
class. Why? Because, that is what people expect, and that is what MS wants control developers to do. What you have to be concerned with in this situation (especially if the controls you develop aren't sealed), is that someone can override different methods, and like you've seen a lot of people override the init
class to make their code work the way they want it to. You always want to avoid a situation where your code mysteriously doesn't work because they forgot to call a base method, and it isn't documented. When you put it in the CreateChildControls
method what you are telling other developers is: "I'm probably doing something with creating controls here you want to take note of." I would always think to class the base of that method if I override it.
The other great thing about putting it in the CreateChildControls
is that a lot of people probably don't know about it (and the people who do, know what it is used for). So when they inherit from your control and monkey with the Init
and forget to call the base Init
method, your code still "magically" works, and they don't have to fuss with figuring out what went wrong.
Bookmark this page!
ASP.NET Page Life Cycle Overview http://msdn.microsoft.com/en-us/library/ms178472.aspx
精彩评论