weixin_33749242 2009-03-06 13:31 采纳率: 0%
浏览 20

动态添加控件和AJAX

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条回答 默认 最新

  • weixin_33728708 2009-03-06 14:41
    关注
    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记