以编程方式将用户控件添加到页面,同时保留已存在的控件

发布于 2024-10-25 22:13:05 字数 535 浏览 2 评论 0原文

我正在尝试在运行时将用户控件添加到 div 中。我可以毫无问题地添加控件,但它会覆盖以前添加的控件。

基本上,我正在尝试将乘客添加到旅行系统中 - 乘客详细信息位于用户控件中,我事先不知道会有多少人。我有一个添加新乘客按钮,该按钮应将新用户控件附加到 div 中,而不会覆盖以前的乘客。

代码是 c#/.net 4。

我尝试将控制数据保存到视图状态中,然后将其重新添加到新的视图状态中,但这也不起作用。这是我正在使用的代码片段

foreach (Control uc in p_passengers.Controls) {
        Passenger p = uc as Passenger;
        if (p != null) {
            p.SaveValues();            
        }
    }

,但是,p.SaveAs()(仅将控制值写入 ViewState)永远不会被命中。

我确定这只是一些愚蠢的事情,但有什么想法吗?

干杯,伙计们。

I am trying to add a user control into a div at runtime. I can add the control no probelem but it overwrites the previous control added.

Basically, I am trying to add passengers to a travel system - the passenger details are in the user control and I don't know in advance how many there will be. I have an add new passenger button which should append the new user control into the div without overwriting the previous passenger.

The code is c#/.net 4.

I have tried to save the control data into viewstate and re add it with the new one but that also doesn't work. Here is a snippet of the code I'm using

foreach (Control uc in p_passengers.Controls) {
        Passenger p = uc as Passenger;
        if (p != null) {
            p.SaveValues();            
        }
    }

however, p.SaveAs() (just writes the control values into ViewState) is never hit.

Im sure its just something stupid but any ideas??

Cheers guys.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱花细雨 2024-11-01 22:13:06

您是否为每次回发重新创建所有动态控件

请记住,每个回发都是 Page 类的一个新实例,并且您之前创建的任何控件都需要显式地重新创建。

更新

如果您在视图状态中有一个添加项目的列表,类似这样..

    private List<string> Items
    {
         get
         {
              return ViewState["Items"] = (ViewState["Items"] ?? new List<string>());
         }
    }

然后在您的点击处理程序中,您可以简单地添加到此列表:

   private void btn_Click(object sender, EventArgs e)
   {
        this.Items.Add("Another Item");
   }

然后覆盖 CreateChildControls

  protected overrides CreateChildControls()
  {
       foreach (string item in this.Items)
       {
            Passanger p = new Passenger();
            p.Something = item;
            this.p_passengers.Controls.Add(p);
       }
  }

Are you re-creating all of your dynamic controls for every postback?

Remember each postback is a new instance of the Page class and any controls you previously created will need to be explicitly re-created.

Update

If you had a list of added items in viewstate, something like this..

    private List<string> Items
    {
         get
         {
              return ViewState["Items"] = (ViewState["Items"] ?? new List<string>());
         }
    }

Then in your click handler you could simply add to this list :

   private void btn_Click(object sender, EventArgs e)
   {
        this.Items.Add("Another Item");
   }

Then override CreateChildControls

  protected overrides CreateChildControls()
  {
       foreach (string item in this.Items)
       {
            Passanger p = new Passenger();
            p.Something = item;
            this.p_passengers.Controls.Add(p);
       }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文