tags:

views:

253

answers:

2

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.

+2  A: 
  1. When you add the controls the first time, keep track of the number of controls you need to recreate in the viewstate.
  2. On every post-back after that make sure you add that number of controls back onto the page with the same IDs in the LoadViewState method.

The key is that you always have to add dynamically created controls to the page every post-back and you have to do it before the viewstate loads in order for the controls to get their form-posted values loaded back into them.

DancesWithBamboo
I never enter the LoadViewState method, how come?Do I have to do anything perticular to raise that event?
The real napster
A: 
BookShowing bs = (BookShowing)UpdatePanel1.FindControl("showing_" + i);

Was the solution, The OnInit override did actually work, it was me fetching the usercontrol the wrong way.

Closed.

The real napster