Troubles with dynamically created WUC

see07

Regular
Joined
Apr 16, 2004
Messages
78
Location
Mexico City
I have a web form, which has a place holder named Example when, Page_Load happens in my web form. I’m creating dynamically a WUC named WebUserControl3 into my place holder.
WebUserControl3 has several text boxes, a button and a label, when user click button I’m executing some calculation which result I’m placing into label. But when user click button occurs Page_Load in web form that contains my place holder and I need recreate my WUC plus data was contained therein, I have tried with ViewState and Session variables but still I can’t pass data from WUC to web form to recreate WUC and its data. Obviously WUC is created again without data. Notwithstanding if I re-enter data in WUC and click button again data are displayed OK.
Why first time it isn’t working and begin second try it’s working fine?
My code is:

private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
}
else
{
Example.Controls.Clear();
Control control = this.Page.LoadControl("WebUserControl3.ascx");
Example.Controls.Add(control);
}
}

Thanks in advance

A.L.
 
Hello:
At last I found a solution.
For some reason the page does not create it with the same control Id. the first time as every other time.
To solve it I inserted this line:
control.ID="WebUserCotrol3"; as it shows:

Example.Controls.Clear();
Control control = this.Page.LoadControl("WebUserControl3.ascx");
control.ID="WebUserCotrol3";
Example.Controls.Add(control);

I hope this be useful for newbie person as I am.

Greetings.

A.L.
 
Back
Top