In my main .aspx page I have a placeholder in which it get registered dynamically two types of user controls.
case "1":
var control = (ControlType)LoadControl("~/PathToControl.ascx");
control.Property = value;
control.Initialize();
placeholder.controls.add(control);
break;
case "2":
var control1 = (ControlType)LoadControl("~/PathToControl1.ascx");
control1.Property = value;
control1.Initialize();
placeholder.controls.add(control1);
break;
Each control has some user controls.
Now, in the main page I have a submit button and I want to get the values of each user coltrol from the above custom control.
I tried to get the controls of the placeholder but the placeholder after the postback is empty.
if(placeholder.Controls.count>0){
var userControl = ((UserControlType)placeholder.Controls[0]);
var controlName = userControl.FindControl("ControlName") as TextBox;
}
I tried also to build the custom controls on page_init but I still can’t get the user controls.
Does anyone know how can I achieve this?
SOLUTION
The dynamic user controls must be set in Init Event on each and every visit.
For more information http://msdn.microsoft.com/en-us/library/ms972976.aspx#viewstate_topic4
The controls must be registered in Init event
set whatever properties you want after the Init
After page’s post back