Home All Groups Group Topic Archive Search About

Reading a public variable from a web user control

Author
14 Jan 2006 8:51 PM
Vi
Hi,
I created a web user control that works fine to display some data. Now I
want to be able to read a public int variable from the control, but I always
get the value I'm innitilizing the variable with. Meaning I can't get the
value that is set within the control.
Here's the code I use:
Control ctl1 = LoadControl("../controls/ctl1.ascx");
((MyNamespace.ControlName)ctl1).id = myInitID;
iVarFromTheControl = ((MyNamespace.ControlName)ctl1).iVarFromTheControl;
PlaceHolder1.Controls.Add(ctl1);

This code is in Page_Load and debugging step-by-step I saw that the
control's code is executed only after Page_Load has finished executing. So I
guess that's the problem, but I'm not sure how to solve it.

Thanks.

Author
16 Jan 2006 12:07 AM
Axel Dahmen
You need to move the following line to Page_Init():

> Control ctl1 = LoadControl("../controls/ctl1.ascx");

This way ASP.NET can fill that control with its PostBack value before
Page_Load() is called.

HTH,
Axel Dahmen

------------
Show quoteHide quote
"Vi" <V*@discussions.microsoft.com> schrieb im Newsbeitrag
news:8DCC4D29-3D12-4A01-AD91-F9DB847C6EBB@microsoft.com...
> Hi,
> I created a web user control that works fine to display some data. Now I
> want to be able to read a public int variable from the control, but I
always
> get the value I'm innitilizing the variable with. Meaning I can't get the
> value that is set within the control.
> Here's the code I use:
> Control ctl1 = LoadControl("../controls/ctl1.ascx");
> ((MyNamespace.ControlName)ctl1).id = myInitID;
> iVarFromTheControl = ((MyNamespace.ControlName)ctl1).iVarFromTheControl;
> PlaceHolder1.Controls.Add(ctl1);
>
> This code is in Page_Load and debugging step-by-step I saw that the
> control's code is executed only after Page_Load has finished executing. So
I
> guess that's the problem, but I'm not sure how to solve it.
>
> Thanks.
>

Bookmark and Share