Ok, I know, I know! ?This is something you must be wondering like “Why this guy is so scre*** up and he had to blog about this”. But I got into this problem last week and an realized that I have seen this problem and addressed this ..but just forgot about. So why not just blog about this. At least for myself :-). So, forgive me folks!!
Using user control from the ASPX page:-
The goal was to access the user control’s control from the ASPX page. First I started looking into why that control in the user control wont be available in the page load of the ASPX page. I could have debugged it. But I thought enabling tracing in and looking into the traces would be a better idea. So I added the trace switch in the web.config (I could have done this on that page itself, but I just did that in the Web.config.). I use the following tag in the Web.config under <System.Web>:-
<trace enabled=”true” pageOutput=”true”/>
Then I put the some Trace.Write statement in the Page_Load of the user control. Notice this is the method where the menu control is getting created (getting populated with the child control). I put the Trace.Write statement at the end of the Page_Load event handler.
I put the Trace.Write statement in the Page_Load of the ASPX page as well at the end.
Now, when I try accessing the aspx page, It would give all the tracing information right from “PreInIt” to “Render” along with all the information on the controls on this page.
Notice that the Trace.Write statement from the user control is afterthe same from the ASPX page which implies that the page load of the user control is fired after the page_load of the ASPX page and hence the user control’s control collection wont be available in the aspx page.
Now, there is another event named Page_PreRenderwhich fires after the page load and you can tweak the controls before thry get Rendered to the page. You can override that event handler as given below.
Notice that in the below screenshot the user control’s(UserStatus1) control(userMenu) is being accessed and is getting modified. To show that this would work, there is a Trace.Write right at the end of the end of this event handler.
See the Trace.Write statement (highlighted) in between the Begin PreRender and End PreRender.