I'm new to user controls and having a bit of an issue. I've got the user control setup on the page which will be using it. The issue I'm having has to do with the load sequence. You see the main page's Page_Load event (which I understand loads first),
is expecting certain controls in the User Control to be populated already. But those controls are not populated yet because the Page_Load event of the User Control has yet to fire. So what happens is I get a run-time error because those
controls are empty. I don't know if it's possible to force the User Control's Page_load event to fire first or if there is a different approach to this. Any help is gladly appreciated.
Why does your Page_load of the page "Expect" certain controls? If it's an initial load of a screen what would you need it for? Yes you can force it by saying
usercontrol1 = new MyUserControl();
but that replaces any settings you have by default set to the user control
can you explain what it's used for and why you need to do the stuff on "Page_Load"?
Coding is like life, learn from the past and work towards the future, trying to redo past code, ends in lost time and nothing new accomplished.
Why does your Page_load of the page "Expect" certain controls? If it's an initial load of a screen what would you need it for? Yes you can force it by saying
usercontrol1 = new MyUserControl();
but that replaces any settings you have by default set to the user control
can you explain what it's used for and why you need to do the stuff on "Page_Load"?
Okay so in my user control, I have a drop-down which is loading in its Page_load event. I've setup a public get property for the SelectedValue of the drop-down so I can access it from my main page. The problem however is that I have another function that
is called in the Page_Load of the "main" page which is already expecting the drop-down to be loaded and have the appropriately selected value, based on the logic I have in my user control. So the issue is that before I can even load my drop-down, my code
crashes because it hasn't had the chance to load it yet, even though it's expecting it.
Dynamically call your user control on main page load event not statically.
From what I've read, if I load it dynamically then I won't be able to use the Page_Load event of my user control. I believe that's the behavior when using the PlaceHolder control to load dynamically.
Nevermind everyone. I discovered I can use the Init method to accomplish what I needed. I'm also learning how to raise events from my user control. Looking good so far. Thank you all for your responses.
msegura
Member
49 Points
68 Posts
User Control Page Load Event
Jul 07, 2012 01:17 AM|LINK
Hi all,
I'm new to user controls and having a bit of an issue. I've got the user control setup on the page which will be using it. The issue I'm having has to do with the load sequence. You see the main page's Page_Load event (which I understand loads first), is expecting certain controls in the User Control to be populated already. But those controls are not populated yet because the Page_Load event of the User Control has yet to fire. So what happens is I get a run-time error because those controls are empty. I don't know if it's possible to force the User Control's Page_load event to fire first or if there is a different approach to this. Any help is gladly appreciated.
AWAlger
Contributor
2531 Points
540 Posts
Re: User Control Page Load Event
Jul 07, 2012 01:31 AM|LINK
Why does your Page_load of the page "Expect" certain controls? If it's an initial load of a screen what would you need it for? Yes you can force it by saying
usercontrol1 = new MyUserControl();
but that replaces any settings you have by default set to the user control
can you explain what it's used for and why you need to do the stuff on "Page_Load"?
Coding is like life, learn from the past and work towards the future, trying to redo past code, ends in lost time and nothing new accomplished.
Don't forget to mark Answer!!!!!!!
Jatin09
Member
76 Points
39 Posts
Re: User Control Page Load Event
Jul 07, 2012 08:28 AM|LINK
Hi,
Dynamically call your user control on main page load event not statically.
jitendraverm...
Member
191 Points
96 Posts
Re: User Control Page Load Event
Jul 07, 2012 11:12 AM|LINK
Hi, try to use this on page load aspx.cs page on which you are using user control
protected void Page_Load(object sender, EventArgs e)
{
UserControl Uc = (UserControl)Page.LoadControl("fileupload.ascx");
DivUserControl.Controls.Add(Uc);
}
here DivUserControl is the division, in which we kept the user control in aspx page eg:
<div id="DivUserControl" runat="server">
<My:fileupload runat="server" ID="Myfileupload" />
</div>
( here i am just using a fileupload with grid in user control )
dont forget to add the registry on aspx page.
<script id="__changoScript" type="text/javascript">// </script>msegura
Member
49 Points
68 Posts
Re: User Control Page Load Event
Jul 07, 2012 06:02 PM|LINK
Okay so in my user control, I have a drop-down which is loading in its Page_load event. I've setup a public get property for the SelectedValue of the drop-down so I can access it from my main page. The problem however is that I have another function that is called in the Page_Load of the "main" page which is already expecting the drop-down to be loaded and have the appropriately selected value, based on the logic I have in my user control. So the issue is that before I can even load my drop-down, my code crashes because it hasn't had the chance to load it yet, even though it's expecting it.
msegura
Member
49 Points
68 Posts
Re: User Control Page Load Event
Jul 07, 2012 06:26 PM|LINK
From what I've read, if I load it dynamically then I won't be able to use the Page_Load event of my user control. I believe that's the behavior when using the PlaceHolder control to load dynamically.
msegura
Member
49 Points
68 Posts
Re: User Control Page Load Event
Jul 07, 2012 07:29 PM|LINK
Nevermind everyone. I discovered I can use the Init method to accomplish what I needed. I'm also learning how to raise events from my user control. Looking good so far. Thank you all for your responses.