I have a master page containing various usercontrol.And i have a page say mypage.aspx that inherits the master page.Then is it possible to call the usercontrol's .cs file before the mypage.aspx's .cs file?
-
your usercontrol code will always be first called. whatevet code you want to call before other code call. Pleac that code in constructor. – Ashok Rathod Jul 14 '14 at 10:21
-
Do you mean at first the usercontrol file is called then only the file that is run?I dont think that is true because if i put linebreaker in the page_load of both userconrtol file and the aspx file,the bebugger goes to page_load of aspx.cs page then only to page_load of ascx.cs – user35 Jul 14 '14 at 10:29
-
in that case dont u make one function in usercontrol and call first from the aspx page.. – Ashok Rathod Jul 14 '14 at 10:37
-
Yes i have made function in the usercontrol but how do i call that function from the aspx page. – user35 Jul 14 '14 at 10:43
-
make that function public and access it using object of that usercontrol. – Ashok Rathod Jul 14 '14 at 10:59
1 Answers
Your concept of "calling .cs file" is totallly wrong. The code behind (.cs) files contain code that runs along the page lifecycle, which is basically a succession of events that happen one after the other, like Init
, Load
, Click
or Changed
event handlers, PreRender
...
So, what is correct is to ask whether an event handler runs before in the master page, in the page or in a user control. Or which event to choose in an user control so that it runs before some other code in the page.
The order in which the same event is invoked in the page and nested user controls, can be from top to bottom (master to most nested user control) or viceversa. It depends in which the event is. I.e. there are events that run first in the page, and then in the nested controls, and other that do all the contrary: run first in the nested controls and then in the page.
So your question should be more specific, like "I want to run this code of the user control before this other code in the page runs". Then, you simply have to choose the right event handler in each to run it in the correct order. You must also take into account what happens before and after each event is called (for example, in the PreInit
the ViewData
is not available yet).
I have a related answer which explains when to wire up event handlers, and why, which makes reference to a proble similar to yours, but more specific. If you read that answer, and specially the included "Page lifecyle" links, you'll understand what you have to do to solve your problem. Specially the last link, which I reproduce here:
Look at this diagram to see the order of execution of Page & children events: ASP.NET Page Life Cycle Diagram
-
ok that was helpful..umm so how do i call function of usercontrol in the aspx page. – user35 Jul 14 '14 at 11:15
-
Please, be much more specific on what you want to do, and I can give you a concrete answer. I.e. how your user control looks like, awhich function do you need to call, and when. If not, I can't blindly give an answer. You should update (edit) your question. – JotaBe Jul 14 '14 at 11:28
-
Here is the scenerio.I have various usercontrol.They are all included in the masterpage.I have a aspx page that implements the master page.So there is no direct relation of usercontrol with the aspx page rather the usercontrol is loaded via the master page.iethe usercontrol is also included in the aspx page.Now when i run the aspx page,the page_load of aspx.cs is compiled first.What i need is to compile function or page_load of usercontrol ahead of aspx.Is it possibe?if possible how?.I hope the scenerio is clear now. – user35 Jul 15 '14 at 04:31
-
Two comments: 1) it's a bit more clear, however saying that "page_load of aspx.cs is compiled first" is still not very understandable. You should say something more concrete like "The Page_Load event of the page runs before the Page_Load event of the user control, and I need to run that code first. This is the code in this events:
". Believe me, that's necessary to help you 2) please, update the question with this additional info. -- I'm sure there is a solution, and I want to help, but please make it easier. Can you edit the question? I'm not sure if you have permissions to do it
– JotaBe Jul 15 '14 at 07:37 -
My sceneria is exactly like this http://stackoverflow.com/questions/24751494/can-i-call-page-load-event-of-usercontrol-before-page-load-event-of-aspx-page – user35 Jul 15 '14 at 08:31