Dynamic Added Controls and AJAX

Let me begin by pointing out, this is not an easy question. At least he is dead. It is almost impossible to find the answer.

In UpdatePanel, I dynamically add some controls to my control panel.

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 happens in a button event fired from an asynchronous control.

Below these fields, I add the code shown above, I have a button. The button is also placed in the update panel. This button is called: btn_editShowings

Now, when I move on to the btn_editShowings_Click event handler, my dynamic added controls no longer exist. I also tried to catch them in OnInit, but they do not exist there either.

F... ??? , , - , ?

UPDATE:

, Init LoadViewState , .

Button

    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();
    }

init: protected override void OnInit (EventArgs e)       {           base.OnInit();

        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();
        }
    }

:

FindControl("showing_1").Visible = false;

.

" " .

+3
2
  • , , .
  • - , LoadViewState.

, -, , .

+2
BookShowing bs = (BookShowing)UpdatePanel1.FindControl("showing_" + i);

, OnInit , usercontrol.

Closed.

0

Source: https://habr.com/ru/post/1704332/


All Articles