0

May I know if there is any way to invoke a method from the child page(.aspx) after the page load of the user control is finished?

Right now I have an issue of being unable to retrieve the value from the child page because the variables from the user control has not been assigned the value yet.

To put it simply

FROM MY .ASPX FILE

Page_Load(object sender, EventArgs e)
{
 x = getValueFromUserControl();
}

FROM MY USER CONTROL

Page_Load(object sender, EventArgs e)
{ 
int x = getvalueFromDatabase();
}

getValueFromuserControl()
{
return x;
}

Since the ASP.NET Life Cycle goes from the child page(.aspx) page_load -> user control page_load, I am unable to retrieve the value of x.

That said, ideally I would not like to put the function in the child page and call it from the user control as the user control is being used in other pages.

In short, I would like to invoke a method from my .aspx page after the page_load in my user control ends, Thank you!

Alan
  • 15
  • 7

1 Answers1

0

Get the value at a later page event:

protected override void OnLoadComplete(EventArgs e) {
   x = getValueFromUserControl();
}

Unless, of course, there is a specific reason why you must get the value on Page_Load. There are other, probably more appropriate ways to handle this, but without knowing what x is and what you need to do with it, it is hard to give any other advice. For example, maybe the UserControl should fire an event that is handled by the page.

Daryl Young
  • 394
  • 2
  • 11
  • Thanks a lot, I didn't want to post out the actual code since I already have figured out what should x do and when it should be retrieve, as to simplify it. I don't think I have any specific reason on where I should call the method to retrieve x, but I shall go try this out. Thanks! – Alan Apr 28 '15 at 02:55
  • Hi Daryl, thanks for the answer. I guess this answer is acceptable since the program does what it should do. Just out of curiosity, what is an example of an appropriate way that this can be handled? (e.g. in the page_load?) – Alan Apr 28 '15 at 03:01
  • Well, I didn't mean that it needed to be handled in Page_Load to be appropriate. It's probably because you simplified the code down, but it seemed odd to be getting a value from DB on load of a UserControl and then calling a method on the UserControl from the container page to get the value. – Daryl Young Apr 28 '15 at 03:16
  • Oh, that user control is used as a view page. So no manipulation or whatsoever is done to the variables in the user control. However this user control has to be present in other pages i.e. updating information. Although to be honest, I have no say over whether or not the user control should be used, just doing my job.. hahaha. Thanks again! – Alan Apr 28 '15 at 03:33