|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Reading a public variable from a web user controlHi,
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. 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 beforePage_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. >
Other interesting topics
CreateChildControls() always returns default property values
detailsview, 3-Tier Binding Problem ????????? Enabling/diusabling child controls Input fields are renamed with runat="server", how do I change it DataGrid dynamic columns / firing events button's OnClick event runs after page databinding... Accessing Datagrid Cells on OnClick Postback Adding OnClick() to an asp:Table Problem Retrieving Values from CheckboxList nested inside Datalist Control |
|||||||||||||||||||||||