Hi Stakcoverflowers
Let me start with pointing out, this is not an easy question to answer. At least it's dead near impossible to find the answer.
In an UpdatePanel I dynamically add some controls to a panel control of mine.
List<Showing> showings = cBLL.GetShowings(tenant.Id);
int j = 1;
foreach(Showing showing in showings)
{
UserControl uc = (UserControl)Page.LoadControl("Controls/BookShowing.ascx");
uc.ID = "showing_" + j;
uc.Visible = true;
((BookShowing)uc).SetShowing(showing);
pnl_showings.Controls.Add(uc);
j++;
}
This all takes place in a button event fired from a control asychrone.
Below these fields I add in the code shown above I have a button. The button is also placed in the updatepanel. This button is called: btn_editShowings
Now when I come to the btn_editShowings_Click event handler, my dynamic added controls does not exist anymore. I have also tried catching them in the OnInit but they dont exist there either.
How the F... is it ever possible to obtains data from the dynamic added controls??? Is there anyway, and I don't care how lousy it performs or anything, to solve this?
UPDATE:
I have now tried to do the following which should work as Init fires before LoadViewState from what I have read.
I add some controls dynamic in a Button event
protected void Button2_Click(object sender, EventArgs e)
{
for (int i = j; i < showno + 4; i++)
{
UserControl uc = (UserControl)Page.LoadControl("Controls/BookShowing.ascx");
uc.ID = "showing_" + i;
uc.Attributes.Add("runat", "Server");
uc.EnableViewState = true;
uc.Visible = true;
pnl_showings.Controls.Add(uc);
}
UpdatePanel1.Update();
}
And I have done the same thing in my init function: protected override void OnInit(EventArgs e) { base.OnInit(e);
if (Session["ShowingsCount"] != null)
{
int noOfUCs = (int)Session["ShowingsCount"];
for (int i = 1; i < noOfUCs; i++)
{
UserControl uc = (UserControl)Page.LoadControl("Controls/BookShowing.ascx");
uc.ID = "showing_" + i;
uc.Attributes.Add("runat", "Server");
uc.EnableViewState = true;
uc.Visible = true;
pnl_showings.Controls.Add(uc);
}
UpdatePanel1.Update();
}
}
But when I try this:
FindControl("showing_1").Visible = false;
I get a null reference exception.
Best Regards The Real Napster, troubled once again.