i always saw that whenever we drag any control on design surface then its access modifier become protected….why it is protected by default…why not private or public.
i got few answers from few people
1) ts beacuse of the scope of of the protected access modifier, as we know,A protected member is accessible from within the class in which it is declared, and from within any class derived from the class that declared this member.and we know that every .net control is internally a class.
A protected member of a base class is accessible in a derived class only if the access takes place through the derived class type.
2) It is protected due to inheritence between code behind and the aspx.cs file. If you dont want it to be inherited you can make it private or sealed
3) i think u point toward right direction….would u plzz come with a sample code just to show the relation between aspx and code behind and the usage of protected modifier which is important.
no answer gave in detail. so please tell me in detail what kind of relation is there as a result any control’s access modifier is protected?
Typically, when ASP.NET compiles aspx page, the generated page class is inherited from the code-behind class. The code that instantiates actual controls and build the control tree resides in the generated class. Because of such relation-ship, the designer by default assigns the
protectedscope to the control variables that gets declared in designer.cs file (which is essentially same as code-behind class). Because of protected scope, these variables become accessible in the actual page class (generated by ASP.NET compiler) and hence the page class code can actually initialize them to correct control instances.The public scope will also work but it violates encapsulation because typically control instances on the page are not needed to be referred out side the page class. The
privatescope will not work because it means the control variables would be accessible only within the code-behind class and hence, the page class (which gets derived from code-behind class) cannot set the variable to the control instance. In such case, you will find that the control variable will have null value in the code-behind class.