修改ASP.NET控件树动态控件、动态、ASP、NET

2023-09-06 05:55:31 作者:你是大姐姐

我想放弃,动态控件添加到我的控件树。例如:

I am trying to drop and add controls dynamically to my control tree. For example:

在特定条件下,我呼吁:

Under a specific condition, I am calling:

    private void resetPanel()
    {
        Panel.Controls.Clear();
        Panel.Controls.Add(Image);
        Panel.Controls.Add(HiddenField);
    }

我的主要目标是如何获得的额外控制在回传坚持?

My main objective is how do I get the added controls to persist across postbacks?

当我打电话使用文本框和标题另一个类似的功能,它坚持完美。然而,随着图像它失去它的URL和属性。

When I call another similar function using textboxes and titles, it persists perfectly. However, with the image it loses its URL and properties.

据我所知,动态控制,以坚持,你必须在初始化加它,你必须负责控制树因此需要以动态控件添加到树的每一个回发。

I understand that for dynamic controls to persist, you must add it on the Init, and you must be responsible for the control tree thus needing to add the dynamic control to the tree on every postback.

那么,为什么它的文本框和标签来后背上坚持工作,但你不能这样做的控件添加图像和hiddenfields?

So why does it work for textboxes and labels persisting across post backs but you cannot do the control add for images and hiddenfields?

谢谢, 布莱恩

- 更新和解决方案 -

我发现在我的code一个错误,并且HiddenField值都坚持在后期背上。我选择了解决的办法是使用ViewState中保存的值,然后恢复我的动态控制每个回发。

I have found a mistake in my code, and the HiddenField values do persist across post backs. The solution I have opted for is to use the ViewState to save the values, then restore my dynamic controls on each post back.

- 编辑 -

感谢您的答复,并因为有可能是我的问题更好的解决方案,这里有一些code,希望能证明我是如何调用该方法,为什么我需要。

Thank you for the replies, and since there may be a better solution to my problem, here is some code that will hopefully show how I am calling the method and why I would need to.

    public void resetTitlePanel()
    {
        // Restylize the panel to initial state
        TitlePanel.Controls.Clear();
        TitlePanel.BorderColor = System.Drawing.Color.Maroon;
        TitlePanel.BorderStyle = BorderStyle.Dashed;
        TitlePanel.Enabled = true;

        // Set the new control properties to initial state
        Label TitleLabel = new Label();
        TitleLabel.ID = "TitleLabel";

        TextBox TitleTxtBox = new TextBox();
        TitleTxtBox.ID = "TitleTxtBox";

        // Add the new controls to the container
        TitlePanel.Controls.Add(TitleLabel);
        TitlePanel.Controls.Add(TitleTxtBox);

        // Set the reference of this to the new dynamic control
        this.TitleLabel = TitleLabel;
        this.TitleTxtBox = TitleTxtBox;
    }

    public void resetImagePanel()
    {
        // Restylize the panel to initial state
        ImagePanel.Controls.Clear();
        ImagePanel.BorderColor = System.Drawing.Color.Blue;
        ImagePanel.BorderStyle = BorderStyle.Dashed;
        ImagePanel.HorizontalAlign = HorizontalAlign.NotSet;

        // Set the new control properties to initial state
        Image AddImage = new Image();
        AddImage.ImageUrl = "~/Resources/Icons/picture_add.png";
        AddImage.ID = "AddImage";

        HiddenField HiddenImage = new HiddenField();
        HiddenImage.ID = "HiddenImage";

        // Add the new controls to the container
        ImagePanel.Controls.Add(AddImage);
        ImagePanel.Controls.Add(HiddenImage);

        // Set the reference of this to the new dynamic control
        this.AddImage = AddImage;
        this.HiddenImage = HiddenImage;
    }

调用方法:

private void copyFromSlide(TemplateControl destination, Template source)
    {
        // Reset the template
        destination.resetTitlePanel();
        destination.resetImagePanel();

        destination.Title = source.Title;
        // Find the path from the database and assign it to the control
        destination.ImagePath = modData.getImagePath((int)source.ImageID);
    }

所以......我的理解是,code是复杂的,也许比它应该是。此外,我只是一个初学者所以它可能是质量更差,我对此表示道歉。

So... I understand that the code is complex, perhaps more than it should be. Further, I am just a beginner so it may be of worse quality, and I apologize for that.

重点提示是:

但是也有一些相互交融2用户控件。 在这工作完全没问题的!的IsPostBack。 的ViewStateEnable是默认的事实,即使我真的明确地分配给它,我得到了相同的结果。 在这个工程完全为题板上它由一个标签和文本框,它们都保留了它的价值。 在我知道我混的静态和动态控制起来。我习惯了C,所以我不能确定我是否可以在对象的指针刚好移动到新的动态对象。

问题是,分配图像路径时,该值不保留在回发。

The problem is, when assigning the image path, the value does not retain on postback.

我要下降,因为具体情况我会下降控件和添加标签,这正如在重新添加控件,都没有问题。所以我认为,我不需要重新初始化控制的原因是因为我加入到根面板具体表现为:

I need to drop and re-add controls because under specific conditions I will drop the controls and add labels, which as noted, have no problem. The reason why I believe that I do not need to initialize the controls over again is because I am adding to a rooted panel as demonstrated by:

http://weblogs.asp.net/infinitiesloop/archive/2006/08/30/TRULY-Understanding-Dynamic-Controls-_2800_Part-3_2900_.aspx

我希望这会增加一些透明度。

I hope this adds some clarity.

再次感谢,

-Brian

推荐答案

ViewState中不跟踪更改,直到InitComplete事件触发。如果更改,并将它们在ViewState中存储在此之前(例如,在初始化),这些变化将不会产生新的视图状态的关键。初始化应该被用来重建从视图状态的控制。在init事件之后的事件是,你应该为这些控件的视图状态设置新值,以便它们将被保存了。

ViewState does not track changes until the InitComplete event fires. If you make changes and store them in ViewState before then (for example in Init) these changes will not generate a new viewstate key. Init should be used to reconstruct your control from viewstate. Events after the Init event is where you should set new values for these controls in view state so that they will be persisted back.

在回答你的第二个问题:影像和HiddenField控件没有任何响应事件(如点击事件),因此并不需要重建。这就是为什么你看到的文本框和标签控件VS图像和HiddenField控件之间的不同behavoir。

In response to your second question: The Image and HiddenField controls do not respond to any events (e.g. click event) and therefore do not need to be reconstructed. This is why you are seeing the different behavoir between the TextBox and Label controls vs the Image and HiddenField controls.

有可能会更容易存储数据,用于确定何时在每一页负载在页面视图状态添加和删除这些控制和刚刚重新创建控件。

It may be easier to store the data that determines when to add and remove those controls in the page view state and just recreate the controls on every page load.